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: ups.com...
> > >>> 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),checkb ox)
> > >>>>
> > >>>> if cb.selected = true then
> > >>>>
> > >>>> '//do domething
> > >>>>
> > >>>> end if
> > >>>>
> > >>>> HTH
> > >>>>
> > >>>>
> > >>>> <> wrote in message
> > >>>> news: ps.com...
> > >>>>> 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.
|