checkbox on gridview

Discussion in 'ASP .Net' started by mike7510uk@yahoo.co.uk, Dec 12, 2006.

  1. Guest

    Hi, I am using a gridview with a templatefield containing a checkbox.
    I want to update the database with a 1 or 0 depending on if a checkbox
    is checked or unchecked (then use the 1 or 0 later on another page)..i
    have been google around and found this....

    If e.Row.RowType = DataControlRowType.DataRow Then

    Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")

    just dont know what to do next...Any ideas?
    , Dec 12, 2006
    #1
    1. Advertising

  2. Goofy Guest

    Well, how you do it depends on when you want to do the update. Basically you
    need to indeed find the control in the row/column. When data is bound to the
    grid, this event fires for each row bound to the control. The event args 'e'
    is passed to the function and you can use this to check if the checkbox is
    checked or not.

    (Not tested but something like this)

    '//e contains the Item which is the current row.
    dim cb as checkbox

    cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)

    if cb.selected = true then

    '//do domething

    end if

    HTH


    <> wrote in message
    news:...
    > Hi, I am using a gridview with a templatefield containing a checkbox.
    > I want to update the database with a 1 or 0 depending on if a checkbox
    > is checked or unchecked (then use the 1 or 0 later on another page)..i
    > have been google around and found this....
    >
    > If e.Row.RowType = DataControlRowType.DataRow Then
    >
    > Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    >
    > just dont know what to do next...Any ideas?
    >
    Goofy, Dec 12, 2006
    #2
    1. Advertising

  3. mike7510uk Guest

    Hi
    I have expanded on what i had originally but still cant get it to
    update DB...here is the new code i have....

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    GridView1.RowDataBound



    Dim MyConnection As OdbcConnection

    MyConnection = New OdbcConnection() 'declare new connection
    object

    MyConnection.ConnectionString = "" 'database connect string

    MyConnection.Open() 'open connection to database

    Dim MyCommand As New OdbcCommand() 'declare new command object

    MyCommand.Connection = MyConnection



    If e.Row.RowType = DataControlRowType.DataRow Then

    Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")



    If Chk.Checked = True Then
    MyCommand.CommandText = "update support.support set
    save = 1"



    End If

    If Chk.Checked = False Then
    MyCommand.CommandText = "update support.support set
    save = 0"



    End If


    MyCommand.ExecuteNonQuery()

    MyConnection.Close()

    MyCommand = Nothing



    'MsgBox(Chk.Checked)



    End If

    End Sub











    Goofy wrote:
    > Well, how you do it depends on when you want to do the update. Basically you
    > need to indeed find the control in the row/column. When data is bound to the
    > grid, this event fires for each row bound to the control. The event args 'e'
    > is passed to the function and you can use this to check if the checkbox is
    > checked or not.
    >
    > (Not tested but something like this)
    >
    > '//e contains the Item which is the current row.
    > dim cb as checkbox
    >
    > cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    >
    > if cb.selected = true then
    >
    > '//do domething
    >
    > end if
    >
    > HTH
    >
    >
    > <> wrote in message
    > news:...
    > > Hi, I am using a gridview with a templatefield containing a checkbox.
    > > I want to update the database with a 1 or 0 depending on if a checkbox
    > > is checked or unchecked (then use the 1 or 0 later on another page)..i
    > > have been google around and found this....
    > >
    > > If e.Row.RowType = DataControlRowType.DataRow Then
    > >
    > > Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    > >
    > > just dont know what to do next...Any ideas?
    > >
    mike7510uk, Dec 12, 2006
    #3
  4. Goofy Guest

    breakpoint the line as shown below and see if you get an exception, if you
    do look at the exception message and see what its telling you and go from
    there.

    Try

    MyCommand.ExecuteNonQuery() '// breakpoint it here


    Catch ex as Exception



    End Try




    "mike7510uk" <> wrote in message
    news:...
    > Hi
    > I have expanded on what i had originally but still cant get it to
    > update DB...here is the new code i have....
    >
    > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > GridView1.RowDataBound
    >
    >
    >
    > Dim MyConnection As OdbcConnection
    >
    > MyConnection = New OdbcConnection() 'declare new connection
    > object
    >
    > MyConnection.ConnectionString = "" 'database connect string
    >
    > MyConnection.Open() 'open connection to database
    >
    > Dim MyCommand As New OdbcCommand() 'declare new command object
    >
    > MyCommand.Connection = MyConnection
    >
    >
    >
    > If e.Row.RowType = DataControlRowType.DataRow Then
    >
    > Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")
    >
    >
    >
    > If Chk.Checked = True Then
    > MyCommand.CommandText = "update support.support set
    > save = 1"
    >
    >
    >
    > End If
    >
    > If Chk.Checked = False Then
    > MyCommand.CommandText = "update support.support set
    > save = 0"
    >
    >
    >
    > End If
    >
    >
    > MyCommand.ExecuteNonQuery()
    >
    > MyConnection.Close()
    >
    > MyCommand = Nothing
    >
    >
    >
    > 'MsgBox(Chk.Checked)
    >
    >
    >
    > End If
    >
    > End Sub
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > Goofy wrote:
    >> Well, how you do it depends on when you want to do the update. Basically
    >> you
    >> need to indeed find the control in the row/column. When data is bound to
    >> the
    >> grid, this event fires for each row bound to the control. The event args
    >> 'e'
    >> is passed to the function and you can use this to check if the checkbox
    >> is
    >> checked or not.
    >>
    >> (Not tested but something like this)
    >>
    >> '//e contains the Item which is the current row.
    >> dim cb as checkbox
    >>
    >> cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    >>
    >> if cb.selected = true then
    >>
    >> '//do domething
    >>
    >> end if
    >>
    >> HTH
    >>
    >>
    >> <> wrote in message
    >> news:...
    >> > Hi, I am using a gridview with a templatefield containing a checkbox.
    >> > I want to update the database with a 1 or 0 depending on if a checkbox
    >> > is checked or unchecked (then use the 1 or 0 later on another page)..i
    >> > have been google around and found this....
    >> >
    >> > If e.Row.RowType = DataControlRowType.DataRow Then
    >> >
    >> > Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    >> >
    >> > just dont know what to do next...Any ideas?
    >> >

    >
    Goofy, Dec 12, 2006
    #4
  5. mike7510uk Guest

    Tried that and nothing happened, it loaded page up ok and let me click
    the checkboxes but no DB update
    Goofy wrote:
    > breakpoint the line as shown below and see if you get an exception, if you
    > do look at the exception message and see what its telling you and go from
    > there.
    >
    > Try
    >
    > MyCommand.ExecuteNonQuery() '// breakpoint it here
    >
    >
    > Catch ex as Exception
    >
    >
    >
    > End Try
    >
    >
    >
    >
    > "mike7510uk" <> wrote in message
    > news:...
    > > Hi
    > > I have expanded on what i had originally but still cant get it to
    > > update DB...here is the new code i have....
    > >
    > > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > > GridView1.RowDataBound
    > >
    > >
    > >
    > > Dim MyConnection As OdbcConnection
    > >
    > > MyConnection = New OdbcConnection() 'declare new connection
    > > object
    > >
    > > MyConnection.ConnectionString = "" 'database connect string
    > >
    > > MyConnection.Open() 'open connection to database
    > >
    > > Dim MyCommand As New OdbcCommand() 'declare new command object
    > >
    > > MyCommand.Connection = MyConnection
    > >
    > >
    > >
    > > If e.Row.RowType = DataControlRowType.DataRow Then
    > >
    > > Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")
    > >
    > >
    > >
    > > If Chk.Checked = True Then
    > > MyCommand.CommandText = "update support.support set
    > > save = 1"
    > >
    > >
    > >
    > > End If
    > >
    > > If Chk.Checked = False Then
    > > MyCommand.CommandText = "update support.support set
    > > save = 0"
    > >
    > >
    > >
    > > End If
    > >
    > >
    > > MyCommand.ExecuteNonQuery()
    > >
    > > MyConnection.Close()
    > >
    > > MyCommand = Nothing
    > >
    > >
    > >
    > > 'MsgBox(Chk.Checked)
    > >
    > >
    > >
    > > End If
    > >
    > > End Sub
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > > Goofy wrote:
    > >> Well, how you do it depends on when you want to do the update. Basically
    > >> you
    > >> need to indeed find the control in the row/column. When data is bound to
    > >> the
    > >> grid, this event fires for each row bound to the control. The event args
    > >> 'e'
    > >> is passed to the function and you can use this to check if the checkbox
    > >> is
    > >> checked or not.
    > >>
    > >> (Not tested but something like this)
    > >>
    > >> '//e contains the Item which is the current row.
    > >> dim cb as checkbox
    > >>
    > >> cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    > >>
    > >> if cb.selected = true then
    > >>
    > >> '//do domething
    > >>
    > >> end if
    > >>
    > >> HTH
    > >>
    > >>
    > >> <> wrote in message
    > >> news:...
    > >> > Hi, I am using a gridview with a templatefield containing a checkbox.
    > >> > I want to update the database with a 1 or 0 depending on if a checkbox
    > >> > is checked or unchecked (then use the 1 or 0 later on another page)..i
    > >> > have been google around and found this....
    > >> >
    > >> > If e.Row.RowType = DataControlRowType.DataRow Then
    > >> >
    > >> > Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    > >> >
    > >> > just dont know what to do next...Any ideas?
    > >> >

    > >
    mike7510uk, Dec 12, 2006
    #5
  6. mike7510uk Guest

    am i even running the code in the correct sub?


    mike7510uk wrote:
    > Tried that and nothing happened, it loaded page up ok and let me click
    > the checkboxes but no DB update
    > Goofy wrote:
    > > breakpoint the line as shown below and see if you get an exception, if you
    > > do look at the exception message and see what its telling you and go from
    > > there.
    > >
    > > Try
    > >
    > > MyCommand.ExecuteNonQuery() '// breakpoint it here
    > >
    > >
    > > Catch ex as Exception
    > >
    > >
    > >
    > > End Try
    > >
    > >
    > >
    > >
    > > "mike7510uk" <> wrote in message
    > > news:...
    > > > Hi
    > > > I have expanded on what i had originally but still cant get it to
    > > > update DB...here is the new code i have....
    > > >
    > > > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > > > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > > > GridView1.RowDataBound
    > > >
    > > >
    > > >
    > > > Dim MyConnection As OdbcConnection
    > > >
    > > > MyConnection = New OdbcConnection() 'declare new connection
    > > > object
    > > >
    > > > MyConnection.ConnectionString = "" 'database connect string
    > > >
    > > > MyConnection.Open() 'open connection to database
    > > >
    > > > Dim MyCommand As New OdbcCommand() 'declare new command object
    > > >
    > > > MyCommand.Connection = MyConnection
    > > >
    > > >
    > > >
    > > > If e.Row.RowType = DataControlRowType.DataRow Then
    > > >
    > > > Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")
    > > >
    > > >
    > > >
    > > > If Chk.Checked = True Then
    > > > MyCommand.CommandText = "update support.support set
    > > > save = 1"
    > > >
    > > >
    > > >
    > > > End If
    > > >
    > > > If Chk.Checked = False Then
    > > > MyCommand.CommandText = "update support.support set
    > > > save = 0"
    > > >
    > > >
    > > >
    > > > End If
    > > >
    > > >
    > > > MyCommand.ExecuteNonQuery()
    > > >
    > > > MyConnection.Close()
    > > >
    > > > MyCommand = Nothing
    > > >
    > > >
    > > >
    > > > 'MsgBox(Chk.Checked)
    > > >
    > > >
    > > >
    > > > End If
    > > >
    > > > End Sub
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > > Goofy wrote:
    > > >> Well, how you do it depends on when you want to do the update. Basically
    > > >> you
    > > >> need to indeed find the control in the row/column. When data is bound to
    > > >> the
    > > >> grid, this event fires for each row bound to the control. The event args
    > > >> 'e'
    > > >> is passed to the function and you can use this to check if the checkbox
    > > >> is
    > > >> checked or not.
    > > >>
    > > >> (Not tested but something like this)
    > > >>
    > > >> '//e contains the Item which is the current row.
    > > >> dim cb as checkbox
    > > >>
    > > >> cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    > > >>
    > > >> if cb.selected = true then
    > > >>
    > > >> '//do domething
    > > >>
    > > >> end if
    > > >>
    > > >> HTH
    > > >>
    > > >>
    > > >> <> wrote in message
    > > >> news:...
    > > >> > Hi, I am using a gridview with a templatefield containing a checkbox.
    > > >> > I want to update the database with a 1 or 0 depending on if a checkbox
    > > >> > is checked or unchecked (then use the 1 or 0 later on another page)..i
    > > >> > have been google around and found this....
    > > >> >
    > > >> > If e.Row.RowType = DataControlRowType.DataRow Then
    > > >> >
    > > >> > Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    > > >> >
    > > >> > just dont know what to do next...Any ideas?
    > > >> >
    > > >
    mike7510uk, Dec 12, 2006
    #6
  7. mike7510uk Guest

    with the code you posted earlier ('//e contains the Item which is the
    current row.
    dim cb as checkbox


    cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)


    if cb.selected = true then


    '//do domething


    end if )

    i get the error saying "item is not a member of system.web etc etc



    mike7510uk wrote:
    > am i even running the code in the correct sub?
    >
    >
    > mike7510uk wrote:
    > > Tried that and nothing happened, it loaded page up ok and let me click
    > > the checkboxes but no DB update
    > > Goofy wrote:
    > > > breakpoint the line as shown below and see if you get an exception, if you
    > > > do look at the exception message and see what its telling you and go from
    > > > there.
    > > >
    > > > Try
    > > >
    > > > MyCommand.ExecuteNonQuery() '// breakpoint it here
    > > >
    > > >
    > > > Catch ex as Exception
    > > >
    > > >
    > > >
    > > > End Try
    > > >
    > > >
    > > >
    > > >
    > > > "mike7510uk" <> wrote in message
    > > > news:...
    > > > > Hi
    > > > > I have expanded on what i had originally but still cant get it to
    > > > > update DB...here is the new code i have....
    > > > >
    > > > > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > > > > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > > > > GridView1.RowDataBound
    > > > >
    > > > >
    > > > >
    > > > > Dim MyConnection As OdbcConnection
    > > > >
    > > > > MyConnection = New OdbcConnection() 'declare new connection
    > > > > object
    > > > >
    > > > > MyConnection.ConnectionString = "" 'database connect string
    > > > >
    > > > > MyConnection.Open() 'open connection to database
    > > > >
    > > > > Dim MyCommand As New OdbcCommand() 'declare new command object
    > > > >
    > > > > MyCommand.Connection = MyConnection
    > > > >
    > > > >
    > > > >
    > > > > If e.Row.RowType = DataControlRowType.DataRow Then
    > > > >
    > > > > Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")
    > > > >
    > > > >
    > > > >
    > > > > If Chk.Checked = True Then
    > > > > MyCommand.CommandText = "update support.support set
    > > > > save = 1"
    > > > >
    > > > >
    > > > >
    > > > > End If
    > > > >
    > > > > If Chk.Checked = False Then
    > > > > MyCommand.CommandText = "update support.support set
    > > > > save = 0"
    > > > >
    > > > >
    > > > >
    > > > > End If
    > > > >
    > > > >
    > > > > MyCommand.ExecuteNonQuery()
    > > > >
    > > > > MyConnection.Close()
    > > > >
    > > > > MyCommand = Nothing
    > > > >
    > > > >
    > > > >
    > > > > 'MsgBox(Chk.Checked)
    > > > >
    > > > >
    > > > >
    > > > > End If
    > > > >
    > > > > End Sub
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > > Goofy wrote:
    > > > >> Well, how you do it depends on when you want to do the update. Basically
    > > > >> you
    > > > >> need to indeed find the control in the row/column. When data is bound to
    > > > >> the
    > > > >> grid, this event fires for each row bound to the control. The event args
    > > > >> 'e'
    > > > >> is passed to the function and you can use this to check if the checkbox
    > > > >> is
    > > > >> checked or not.
    > > > >>
    > > > >> (Not tested but something like this)
    > > > >>
    > > > >> '//e contains the Item which is the current row.
    > > > >> dim cb as checkbox
    > > > >>
    > > > >> cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    > > > >>
    > > > >> if cb.selected = true then
    > > > >>
    > > > >> '//do domething
    > > > >>
    > > > >> end if
    > > > >>
    > > > >> HTH
    > > > >>
    > > > >>
    > > > >> <> wrote in message
    > > > >> news:...
    > > > >> > Hi, I am using a gridview with a templatefield containing a checkbox.
    > > > >> > I want to update the database with a 1 or 0 depending on if a checkbox
    > > > >> > is checked or unchecked (then use the 1 or 0 later on another page)..i
    > > > >> > have been google around and found this....
    > > > >> >
    > > > >> > If e.Row.RowType = DataControlRowType.DataRow Then
    > > > >> >
    > > > >> > Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    > > > >> >
    > > > >> > just dont know what to do next...Any ideas?
    > > > >> >
    > > > >
    mike7510uk, Dec 12, 2006
    #7
  8. mike7510uk wrote:
    > Tried that and nothing happened, it loaded page up ok and let me click
    > the checkboxes but no DB update
    > Goofy wrote:
    >> breakpoint the line as shown below and see if you get an exception, if you
    >> do look at the exception message and see what its telling you and go from
    >> there.
    >>
    >> Try
    >>
    >> MyCommand.ExecuteNonQuery() '// breakpoint it here
    >>
    >>
    >> Catch ex as Exception
    >>
    >>
    >>
    >> End Try
    >>
    >>
    >>
    >>
    >> "mike7510uk" <> wrote in message
    >> news:...
    >>> Hi
    >>> I have expanded on what i had originally but still cant get it to
    >>> update DB...here is the new code i have....
    >>>
    >>> Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    >>> System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    >>> GridView1.RowDataBound
    >>>
    >>>
    >>>
    >>> Dim MyConnection As OdbcConnection
    >>>
    >>> MyConnection = New OdbcConnection() 'declare new connection
    >>> object
    >>>
    >>> MyConnection.ConnectionString = "" 'database connect string
    >>>
    >>> MyConnection.Open() 'open connection to database
    >>>
    >>> Dim MyCommand As New OdbcCommand() 'declare new command object
    >>>
    >>> MyCommand.Connection = MyConnection
    >>>
    >>>
    >>>
    >>> If e.Row.RowType = DataControlRowType.DataRow Then
    >>>
    >>> Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")
    >>>
    >>>
    >>>
    >>> If Chk.Checked = True Then
    >>> MyCommand.CommandText = "update support.support set
    >>> save = 1"
    >>>
    >>>
    >>>
    >>> End If
    >>>
    >>> If Chk.Checked = False Then
    >>> MyCommand.CommandText = "update support.support set
    >>> save = 0"
    >>>
    >>>
    >>>
    >>> End If
    >>>
    >>>
    >>> MyCommand.ExecuteNonQuery()
    >>>
    >>> MyConnection.Close()
    >>>
    >>> MyCommand = Nothing
    >>>
    >>>
    >>>
    >>> 'MsgBox(Chk.Checked)
    >>>
    >>>
    >>>
    >>> End If
    >>>
    >>> End Sub
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>> Goofy wrote:
    >>>> Well, how you do it depends on when you want to do the update. Basically
    >>>> you
    >>>> need to indeed find the control in the row/column. When data is bound to
    >>>> the
    >>>> grid, this event fires for each row bound to the control. The event args
    >>>> 'e'
    >>>> is passed to the function and you can use this to check if the checkbox
    >>>> is
    >>>> checked or not.
    >>>>
    >>>> (Not tested but something like this)
    >>>>
    >>>> '//e contains the Item which is the current row.
    >>>> dim cb as checkbox
    >>>>
    >>>> cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    >>>>
    >>>> if cb.selected = true then
    >>>>
    >>>> '//do domething
    >>>>
    >>>> end if
    >>>>
    >>>> HTH
    >>>>
    >>>>
    >>>> <> wrote in message
    >>>> news:...
    >>>>> Hi, I am using a gridview with a templatefield containing a checkbox.
    >>>>> I want to update the database with a 1 or 0 depending on if a checkbox
    >>>>> is checked or unchecked (then use the 1 or 0 later on another page)..i
    >>>>> have been google around and found this....
    >>>>>
    >>>>> If e.Row.RowType = DataControlRowType.DataRow Then
    >>>>>
    >>>>> Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    >>>>>
    >>>>> just dont know what to do next...Any ideas?
    >>>>>

    >


    How are you specifying where to do the update within your support table?
    MyCommand.CommandText = "update support.support set save = 0"

    I'm not an expert, but I couldn't find anything in your code (update sql
    statement) that would tell the database where in the table to do the update.
    Jim in Arizona, Dec 12, 2006
    #8
  9. mike7510uk Guest

    Ok, im confused now..

    Basically, i want a gridview with a templatefield (or checkboxfield)
    with a checkbox on it that when the user clicks the checkbox it sends a
    1 or 0 value back to DB (1 for checked and 0 for unchecked)


    Jim in Arizona wrote:
    > mike7510uk wrote:
    > > Tried that and nothing happened, it loaded page up ok and let me click
    > > the checkboxes but no DB update
    > > Goofy wrote:
    > >> breakpoint the line as shown below and see if you get an exception, if you
    > >> do look at the exception message and see what its telling you and go from
    > >> there.
    > >>
    > >> Try
    > >>
    > >> MyCommand.ExecuteNonQuery() '// breakpoint it here
    > >>
    > >>
    > >> Catch ex as Exception
    > >>
    > >>
    > >>
    > >> End Try
    > >>
    > >>
    > >>
    > >>
    > >> "mike7510uk" <> wrote in message
    > >> news:...
    > >>> Hi
    > >>> I have expanded on what i had originally but still cant get it to
    > >>> update DB...here is the new code i have....
    > >>>
    > >>> Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > >>> System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > >>> GridView1.RowDataBound
    > >>>
    > >>>
    > >>>
    > >>> Dim MyConnection As OdbcConnection
    > >>>
    > >>> MyConnection = New OdbcConnection() 'declare new connection
    > >>> object
    > >>>
    > >>> MyConnection.ConnectionString = "" 'database connect string
    > >>>
    > >>> MyConnection.Open() 'open connection to database
    > >>>
    > >>> Dim MyCommand As New OdbcCommand() 'declare new command object
    > >>>
    > >>> MyCommand.Connection = MyConnection
    > >>>
    > >>>
    > >>>
    > >>> If e.Row.RowType = DataControlRowType.DataRow Then
    > >>>
    > >>> Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")
    > >>>
    > >>>
    > >>>
    > >>> If Chk.Checked = True Then
    > >>> MyCommand.CommandText = "update support.support set
    > >>> save = 1"
    > >>>
    > >>>
    > >>>
    > >>> End If
    > >>>
    > >>> If Chk.Checked = False Then
    > >>> MyCommand.CommandText = "update support.support set
    > >>> save = 0"
    > >>>
    > >>>
    > >>>
    > >>> End If
    > >>>
    > >>>
    > >>> MyCommand.ExecuteNonQuery()
    > >>>
    > >>> MyConnection.Close()
    > >>>
    > >>> MyCommand = Nothing
    > >>>
    > >>>
    > >>>
    > >>> 'MsgBox(Chk.Checked)
    > >>>
    > >>>
    > >>>
    > >>> End If
    > >>>
    > >>> End Sub
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>
    > >>>
    > >>> Goofy wrote:
    > >>>> Well, how you do it depends on when you want to do the update. Basically
    > >>>> you
    > >>>> need to indeed find the control in the row/column. When data is bound to
    > >>>> the
    > >>>> grid, this event fires for each row bound to the control. The event args
    > >>>> 'e'
    > >>>> is passed to the function and you can use this to check if the checkbox
    > >>>> is
    > >>>> checked or not.
    > >>>>
    > >>>> (Not tested but something like this)
    > >>>>
    > >>>> '//e contains the Item which is the current row.
    > >>>> dim cb as checkbox
    > >>>>
    > >>>> cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    > >>>>
    > >>>> if cb.selected = true then
    > >>>>
    > >>>> '//do domething
    > >>>>
    > >>>> end if
    > >>>>
    > >>>> HTH
    > >>>>
    > >>>>
    > >>>> <> wrote in message
    > >>>> news:...
    > >>>>> Hi, I am using a gridview with a templatefield containing a checkbox.
    > >>>>> I want to update the database with a 1 or 0 depending on if a checkbox
    > >>>>> is checked or unchecked (then use the 1 or 0 later on another page)..i
    > >>>>> have been google around and found this....
    > >>>>>
    > >>>>> If e.Row.RowType = DataControlRowType.DataRow Then
    > >>>>>
    > >>>>> Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    > >>>>>
    > >>>>> just dont know what to do next...Any ideas?
    > >>>>>

    > >

    >
    > How are you specifying where to do the update within your support table?
    > MyCommand.CommandText = "update support.support set save = 0"
    >
    > I'm not an expert, but I couldn't find anything in your code (update sql
    > statement) that would tell the database where in the table to do the update.
    mike7510uk, Dec 13, 2006
    #9
  10. mike7510uk Guest

    I have now managed to do this (at last)
    here is the code...

    Protected Sub save_CheckedChanged(ByVal sender As Object, ByVal e As
    System.EventArgs)

    Dim MyConnection As OdbcConnection

    MyConnection = New OdbcConnection() 'declare new connection
    object

    MyConnection.ConnectionString = " " 'database connect string

    MyConnection.Open() 'open connection to database

    Dim MyCommand As New OdbcCommand() 'declare new command object

    MyCommand.Connection = MyConnection

    Dim cID As Integer = 0
    ' Dim str As StringBuilder = New StringBuilder
    Dim i As Integer = 0
    Dim state As Integer
    For i = 0 To GridView1.Rows.Count - 1
    'Dim row As GridViewRow = GridView1.Rows(i)
    Dim isChecked As Boolean =
    CType(GridView1.Rows(i).FindControl("save"), CheckBox).Checked
    If isChecked Then
    state = 1
    Else
    state = 0
    End If

    MyCommand.CommandText = "update support.support set save =?
    where id=?"
    MyCommand.Parameters.Clear()
    MyCommand.Parameters.Add(New OdbcParameter("@state",
    state))
    MyCommand.Parameters.Add(New OdbcParameter("@id",
    GridView1.Rows(i).Cells(0).Text))
    MyCommand.ExecuteNonQuery()
    Next

    MyConnection.Close()
    MyCommand = Nothing
    End Sub

    All i need to do now is if a user checks a particular checkbox, it
    remains checked the next time they visit the page..



    mike7510uk wrote:
    > Ok, im confused now..
    >
    > Basically, i want a gridview with a templatefield (or checkboxfield)
    > with a checkbox on it that when the user clicks the checkbox it sends a
    > 1 or 0 value back to DB (1 for checked and 0 for unchecked)
    >
    >
    > Jim in Arizona wrote:
    > > mike7510uk wrote:
    > > > Tried that and nothing happened, it loaded page up ok and let me click
    > > > the checkboxes but no DB update
    > > > Goofy wrote:
    > > >> breakpoint the line as shown below and see if you get an exception, if you
    > > >> do look at the exception message and see what its telling you and go from
    > > >> there.
    > > >>
    > > >> Try
    > > >>
    > > >> MyCommand.ExecuteNonQuery() '// breakpoint it here
    > > >>
    > > >>
    > > >> Catch ex as Exception
    > > >>
    > > >>
    > > >>
    > > >> End Try
    > > >>
    > > >>
    > > >>
    > > >>
    > > >> "mike7510uk" <> wrote in message
    > > >> news:...
    > > >>> Hi
    > > >>> I have expanded on what i had originally but still cant get it to
    > > >>> update DB...here is the new code i have....
    > > >>>
    > > >>> Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > > >>> System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > > >>> GridView1.RowDataBound
    > > >>>
    > > >>>
    > > >>>
    > > >>> Dim MyConnection As OdbcConnection
    > > >>>
    > > >>> MyConnection = New OdbcConnection() 'declare new connection
    > > >>> object
    > > >>>
    > > >>> MyConnection.ConnectionString = "" 'database connect string
    > > >>>
    > > >>> MyConnection.Open() 'open connection to database
    > > >>>
    > > >>> Dim MyCommand As New OdbcCommand() 'declare new command object
    > > >>>
    > > >>> MyCommand.Connection = MyConnection
    > > >>>
    > > >>>
    > > >>>
    > > >>> If e.Row.RowType = DataControlRowType.DataRow Then
    > > >>>
    > > >>> Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")
    > > >>>
    > > >>>
    > > >>>
    > > >>> If Chk.Checked = True Then
    > > >>> MyCommand.CommandText = "update support.support set
    > > >>> save = 1"
    > > >>>
    > > >>>
    > > >>>
    > > >>> End If
    > > >>>
    > > >>> If Chk.Checked = False Then
    > > >>> MyCommand.CommandText = "update support.support set
    > > >>> save = 0"
    > > >>>
    > > >>>
    > > >>>
    > > >>> End If
    > > >>>
    > > >>>
    > > >>> MyCommand.ExecuteNonQuery()
    > > >>>
    > > >>> MyConnection.Close()
    > > >>>
    > > >>> MyCommand = Nothing
    > > >>>
    > > >>>
    > > >>>
    > > >>> 'MsgBox(Chk.Checked)
    > > >>>
    > > >>>
    > > >>>
    > > >>> End If
    > > >>>
    > > >>> End Sub
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>>
    > > >>> Goofy wrote:
    > > >>>> Well, how you do it depends on when you want to do the update. Basically
    > > >>>> you
    > > >>>> need to indeed find the control in the row/column. When data is bound to
    > > >>>> the
    > > >>>> grid, this event fires for each row bound to the control. The event args
    > > >>>> 'e'
    > > >>>> is passed to the function and you can use this to check if the checkbox
    > > >>>> is
    > > >>>> checked or not.
    > > >>>>
    > > >>>> (Not tested but something like this)
    > > >>>>
    > > >>>> '//e contains the Item which is the current row.
    > > >>>> dim cb as checkbox
    > > >>>>
    > > >>>> cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)
    > > >>>>
    > > >>>> if cb.selected = true then
    > > >>>>
    > > >>>> '//do domething
    > > >>>>
    > > >>>> end if
    > > >>>>
    > > >>>> HTH
    > > >>>>
    > > >>>>
    > > >>>> <> wrote in message
    > > >>>> news:...
    > > >>>>> Hi, I am using a gridview with a templatefield containing a checkbox.
    > > >>>>> I want to update the database with a 1 or 0 depending on if a checkbox
    > > >>>>> is checked or unchecked (then use the 1 or 0 later on another page)..i
    > > >>>>> have been google around and found this....
    > > >>>>>
    > > >>>>> If e.Row.RowType = DataControlRowType.DataRow Then
    > > >>>>>
    > > >>>>> Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")
    > > >>>>>
    > > >>>>> just dont know what to do next...Any ideas?
    > > >>>>>
    > > >

    > >
    > > How are you specifying where to do the update within your support table?
    > > MyCommand.CommandText = "update support.support set save = 0"
    > >
    > > I'm not an expert, but I couldn't find anything in your code (update sql
    > > statement) that would tell the database where in the table to do the update.
    mike7510uk, Dec 13, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    0
    Views:
    505
    tshad
    Apr 15, 2005
  2. Vikram

    disable checkbox list checkbox

    Vikram, Jan 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,377
    ThunderMusic
    Jan 25, 2006
  3. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    799
    David Wier
    Oct 6, 2006
  4. Jason Huang
    Replies:
    0
    Views:
    538
    Jason Huang
    Jun 29, 2007
  5. Joey Pang
    Replies:
    4
    Views:
    505
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page