I have two drop-down menus: one is for the department, and another one - for the associates in this department. I populated the first one from the database. Now I want the second drop down to be populated from the database depending on the value selected in the first drop-down. To execute the second procedure I turned the AutoPostBack property on the first drop-down menu to TRUE to reload the page each time I change the value. It works and populates the associate ids in the second box. The problem appears when I need to send data to the third procedure that uses both variables. Here is a sub for the second drop-down menu:
Sub DisplayID()
Try
strDept = ddlDept.SelectedItem.Value
'Refer to the connection in web.config
strConnection = ConfigurationSettings.AppSettings("DBConnection")
'Connect to the database
sqlConn = New SqlConnection(strConnection)
'Create a command string
strCmd = "sp_PullID"
'Create a command
sqlCmd = New SqlCommand(strCmd, sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
'Set parameters
sqlParam = sqlCmd.Parameters.Add(New SqlParameter("@sDept", SqlDbType.VarChar, 10))
sqlParam.Direction = ParameterDirection.Input
sqlParam.Value = strDept
'Open Connection
sqlConn.Open()
'Set Data Reader
sqlReader = sqlCmd.ExecuteReader()
'Get data from the Data Reader
ddlID.DataSource = sqlReader
ddlID.DataTextField = "sName"
ddlID.DataValueField = "sID"
If Not Page.IsPostBack Then
ddlID.DataBind()
End If
Catch ex As Exception
Response.Write(ex.ToString & "<br>")
Finally
sqlReader.Close()
sqlConn.Close()
End Try
End Sub
If I use IsPostBack, the second drop-down menu is not populated with new names every time I change the value in the first drop-down. If I bind data without IsPostBack, the variable created for the ID does not hold the value that should be passed to the third stored procedure. Is there any way around it?
Sub DisplayID()
Try
strDept = ddlDept.SelectedItem.Value
'Refer to the connection in web.config
strConnection = ConfigurationSettings.AppSettings("DBConnection")
'Connect to the database
sqlConn = New SqlConnection(strConnection)
'Create a command string
strCmd = "sp_PullID"
'Create a command
sqlCmd = New SqlCommand(strCmd, sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
'Set parameters
sqlParam = sqlCmd.Parameters.Add(New SqlParameter("@sDept", SqlDbType.VarChar, 10))
sqlParam.Direction = ParameterDirection.Input
sqlParam.Value = strDept
'Open Connection
sqlConn.Open()
'Set Data Reader
sqlReader = sqlCmd.ExecuteReader()
'Get data from the Data Reader
ddlID.DataSource = sqlReader
ddlID.DataTextField = "sName"
ddlID.DataValueField = "sID"
If Not Page.IsPostBack Then
ddlID.DataBind()
End If
Catch ex As Exception
Response.Write(ex.ToString & "<br>")
Finally
sqlReader.Close()
sqlConn.Close()
End Try
End Sub
If I use IsPostBack, the second drop-down menu is not populated with new names every time I change the value in the first drop-down. If I bind data without IsPostBack, the variable created for the ID does not hold the value that should be passed to the third stored procedure. Is there any way around it?