views:

343

answers:

1

can you populate checkboxlist from sql server query like a dropdownlist? with autopostback=true? i am using vb.net and have 50 checkboxes that shall show up from the database data depending on the selected value of the previous dropdownlist. also can i change the label of the checkbox each time the value is from DB? the label shall be same as the checkbox value.

+1  A: 

Assuming your CheckBoxList's ControlId is myCheckBoxList:

Dim mySQL As String = "Name_of_stored_proceedure"
Dim mySqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection("The_connection_string")
Dim mySqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(mySQL, mySqlConnection)
mySqlCommand.CommandType = CommandType.StoredProcedure      
Dim myDataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(mySqlCommand)
Dim myDataTable As New DataTable
mySqlConnection.Open()
myDataAdapter.Fill(myDataTable)
mySqlConnection.Close()
myCheckBoxList.DataSource = myDataTable
myCheckBoxList.DataBind()

This is the process to use a stored procedure. If you would like to use straight SQL or a parametrized query take out the mySqlCommand.CommandType = CommandType.StoredProcedure and put the SQL in for the "Name_of_stored_proceedure".

Remember to put the column name from the database in the DataValueField property of the CheckBoxList that you would like to for the the value and the column name for the DataTextField you would like to use for the text.

Deathbat