I have a dropdownlist2 in a gridview whose datasource and select control parameter is based on the selected value of dropdownlist1 in the same gridview. Everything works great for insert, but when I edit the row in the gridview and change the selected value in dropdownlist1, I get an error of "'dropdownlist2 ' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value" This kinda makes sense to me. At what stage (maybe drowdownlist1 selectedindexch anged?) do I tell dropdownlist2 that it needs to requery so I don't get the error. I can just tell it to select the 0 index by default here for dropdownlist2. An example would be lovely.
Tried the following on Dropdownlist1.s elected item changed with the same error:
Tried the following on Dropdownlist1.s elected item changed with the same error:
Code:
'Grabbing a reference to the ddl1 that raised the event
Dim ddl1 As DropDownList = CType(sender, DropDownList)
'Grabbing a reference to the GridViewRow that the ddl1 belongs to
'so that I can grab a reference to the ddl2 that belongs to that row
Dim gridViewRowDdlBelongsTo As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
'Grabbing a reference to ddl2 that is in the same row as ddl1
Dim ddl2 As DropDownList = Nothing
If gridViewRowDdlBelongsTo IsNot Nothing Then
ddl2 = CType(gridViewRowDdlBelongsTo.FindControl("roomDDLE"), DropDownList)
End If
'Generating the data source for ddl2 depending on what was selected in ddl1
Dim mySql As String = "select ID, Name from dbo.myDatabase where ID=" & ddl1.SelectedItem.Value.ToString
Dim ds As New Data.DataSet
Dim strConn As String = ConfigurationManager.ConnectionStrings("strConn").ConnectionString
Dim cn As New Data.SqlClient.SqlConnection(strConn)
Dim da As New Data.SqlClient.SqlDataAdapter
da = New Data.SqlClient.SqlDataAdapter(mySql, cn)
da.Fill(ds, "newTable")
'Setting the datasource for ddl2 in the row, and binding to it.
If ddl2 IsNot Nothing Then
ddl2.DataSource = ds.Tables("newTable")
ddl2.DataBind()
ddl2.SelectedIndex = 0
End If