0

Protected Sub subdata(ByVal id As Integer)
Dim myDataReader As System.Data.SqlClient.SqlDataReader
Dim myConnection As New SqlConnection
Dim myCommand As New SqlCommand
Dim strSQL As String
strSQL = "SELECT class_id,subject_id,subject_name FROM subject where subject_id =@class_id "
myConnection = New SqlConnection(" Data Source=SAJJAD;Initial Catalog=ac;Integrated Security=True")
myCommand.Parameters.AddWithValue("@class_id", id)
myCommand = New SqlCommand(strSQL, myConnection)
myConnection.Open()

myDataReader = myCommand.ExecuteReader()

If myDataReader.HasRows Then
Me.class_DropDownList1.DataSource = myDataReader
Me.sub_DropDownList1.DataValueField = "subject_id"
sub_DropDownList1.DataTextField = "subject_name"
sub_DropDownList1.DataBind()
End If

myDataReader.Close()
myConnection.Close()
End Sub

from above code i want to populate the dropdownlist list with getting the value from parameter but i am facing this error

(Must declare the scalar variable "@class_id")

what should i do

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by sknake
0

Hi,
What is @class_id?
You are trying to pass a value to the sql statement but the
variable @class_id does not contain any value.

First you have to declare a variable,then initialize it and then
you can add it as a parameter to the sql statement.

regards

0

An example of adding a parameter:

using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          cmd.Parameters.Add(new SqlParameter("@Loc", Loc));
          cmd.Parameters.Add(new SqlParameter("@Lab", Lab));
          cmd.Parameters.Add(new SqlParameter("@Tot", Tot));
          return (Convert.ToInt32(cmd.ExecuteScalar()) > 0);
        }

You are using a variable reference in your query but not giving the variable to the command:

strSQL = "SELECT class_id,subject_id,subject_name FROM subject where subject_id =@class_id "
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.