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

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

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 "
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.