I have 2 dropdowns, DroDownList1 and DropDownList2. To insert records into the database, I select a value from DropDownList1 and based on specified condition, a value is automatically inserted into DropDownList2. Then when I select a value, say 1, from DropDownList2, one row is automatically created in Repeater control. If I select 2, two rows are automatically created. Finally, if I select 3 again from DropDownList2, three rows are automatically created in Repeater control. Three rows is the maximum number of rows that can be created because 3 values (1,2,3) are the maximum that can be populated into DropDownList2. Then we insert the records into the database. This entire operation works great. Then to retrieve the records we just inserted based on the description above, I use the following method:
Protected Sub btnSearch(ByVal sender As Object, ByVal e As EventArgs)
Dim address As String = Request.Form(txtSearch.UniqueID)
'get the results and fill in the form
Dim sqlStatement As String = "Select ad.returnonly, ad.Assets,ad.NoOfItemsToReplace,ap.MailAddress,o.PrimaryFirst, o.Phone, o.AltPhone,o.Email,o.PrimaryLast, o.ownerID, ap.applicant, FORMAT(ap.DateReceived, 'd','us') as DateReceived,ad.InstallAddress,ad.InstallCity, ad.InstallState, ad.InstallZip from Applications ap "
sqlStatement += "inner Join Addresses ad on ap.WaterAccountNo = ad.WaterAcctNo inner join Owner o on ap.OwnerCode = o.OwnerID Where ad.Installation = 1 and ad.InstallAddress Like '%" & address.Replace("'", "''").Trim() & "%'"
Dim sqlCmd As SqlCommand = New SqlCommand(sqlStatement, myConnection)
Dim reader As SqlDataReader = sqlCmd.ExecuteReader()
If reader.HasRows Then
While reader.Read()
DropDownList1.Text = reader("Assets").ToString()
DropDownList1\_SelectedIndexChanged(Nothing, Nothing)
DropDownList2.Text = reader("NoOfItemsToReplace").ToString()
End While
Else
End If
reader.Close()
sqlCmd.Dispose()
End Sub
The above method populates DropdownList1 values and selects the inserted value by default. For instance, if 2 is selected from the list and inserted into the database, during select statements, 2 will be selected as the default value for DropDownList1. Same is the case with DropDownList2 but more importantly, the below method