Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Savind checkbox list selections to database.

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have the following code which populates a checkbox list control with selected values from a table. This works perfectly fine.

What I now need to do is, if a user selects/deselects a checkbox I want to save these changes to the table.

Help please.

Code:
If Not IsPostBack Then

            Dim strConnString As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
            Dim objConn As New SqlConnection(strConnString)
            Dim objcmd As New SqlCommand()
            objcmd.CommandType = CommandType.StoredProcedure
            objcmd.CommandText = "spGetRoomFeatures"

            objcmd.Connection = objConn

            objConn.Open()
            CheckBoxList1.DataSource = objcmd.ExecuteReader(CommandBehavior.CloseConnection)
            CheckBoxList1.DataBind()

            objcmd = New SqlCommand()
            objcmd.CommandType = CommandType.StoredProcedure
            objcmd.CommandText = "spGetSelectedFeatures"
            objcmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = 1
            objcmd.Connection = objConn


            objConn.Open()
            Dim objReader As SqlDataReader = objCmd.ExecuteReader()

            While objReader.Read()
                Dim currentCheckBox As ListItem = CheckBoxList1.Items.FindByValue(objReader("feature").ToString())
                If currentCheckBox IsNot Nothing Then
                    currentCheckBox.Selected = True
                End If
            End While
        End If
 
Solved with:

Code:
Dim strConnString As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "spAddFeatures"

        cmd.Connection = con

        con.Open()

        For Each item As ListItem In CheckBoxList1.Items

            If item.Selected = True Then
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@RoomID", 1)
                cmd.Parameters.AddWithValue("@Feature", item.Value)
                cmd.ExecuteNonQuery()
            End If

        Next
        con.Close()
 
A "deselected" checkbox does NOT pass a value or a key in the POST collection/array you cannot directly detect such a event serverside, because you have to check for something that isn't actually there.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top