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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkboxlist - gridview

Status
Not open for further replies.

tsp1lrk72

IS-IT--Management
Feb 25, 2008
100
US
hello,

I have a gridview with 2 checkbox lists; I'm editing/updating the database with my new comma delimited string of values. the first part, chkThursEvents, updates with no issues, the second one, chkFridayEvents, does not update at all- not sure what is wrong with the code... help!

Code:
 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim showid As Integer
        showid = GridView1.DataKeys(e.RowIndex).Value
        Dim cblist2 As CheckBoxList
        cblist2 = CType(GridView1.Rows(e.RowIndex).FindControl("chkThursEvents"), CheckBoxList)
        Dim i2
        Dim SendString2 As String = ""
        For i2 = 0 To cblist2.Items.Count - 1
            If cblist2.Items(i2).Selected = False Then
                lblEVThurs.Text = SendString2
                GridView1.DataBind()
            End If
            If cblist2.Items(i2).Selected = True Then
                SendString2 &= cblist2.Items(i2).Value.ToString + ","
                lblEVThurs.Text = SendString2
                Dim conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ENAPWebConnectionString").ConnectionString)
                Dim upcommand As New SqlClient.SqlCommand
                upcommand = conn.CreateCommand
                upcommand.CommandType = CommandType.Text
                upcommand.CommandText = "update tblVendorShowEvents set chkThursEvents='" & SendString2 & "' where showid=" & Trim(showid) & " "
                conn.Open()
                upcommand.ExecuteNonQuery()
                conn.Close()
                GridView1.DataBind()
            End If
        Next

        Dim cblist As CheckBoxList
        showid = GridView1.DataKeys(e.RowIndex).Value
        cblist = CType(GridView1.Rows(e.RowIndex).FindControl("chkFridayEvents"), CheckBoxList)
        Dim i
        Dim SendString As String = ""
        For i = 0 To cblist.Items.Count - 1
            If cblist.Items(i).Selected = False Then
                lblEVFri.Text = SendString
                GridView1.DataBind()
            End If
            If cblist.Items(i).Selected Then
                SendString &= cblist.Items(i).Value.ToString + ","
                lblEVFri.Text = SendString
                Dim conn1 As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ENAPWebConnectionString").ConnectionString)
                Dim upcommand1 As New SqlClient.SqlCommand
                upcommand1 = conn1.CreateCommand
                upcommand1.CommandType = CommandType.Text
                upcommand1.CommandText = "update tblVendorShowEvents set chkFridayEvents='" & SendString & "' where showid=" & Trim(showid) & " "
                conn1.Open()
                upcommand1.ExecuteNonQuery()
                conn1.Close()
                GridView1.DataBind()
            End If
        Next



    End Sub
 
Have you traced through the code?
I don't think this is going to work like you are thinking. You have the buliding of the list of values in a loop with the update, which means the update command will run for each checkbox list item that is checked. What you should do is create a comma separated list of values and sent that to your command, or preferably a stored procedure.
 
The first one, chkThursEvents works like I want it to,takes the comma delimited string and updates the table; it's the second one that won't update anything... weird.
 
You have to trace through the code. The logic is not correct. It will never create the string the way you want. It will continuously call the update for each clicked box. Eg, If you have 3 boxes checked with ids of 1,2,3 this will happen:

SendString = 1, then an update
SendString = 1,2, then an update
SendString = 1,2,3, then an update

You are calling the update multiple times and updating the same rows multiple times. The first one might look like it is working if you are selecting ALL the check boxes. I suspect that somthing will not work correctly if some are NOT checked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top