Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > checkbox on gridview

Reply
Thread Tools

checkbox on gridview

 
 
mike7510uk@yahoo.co.uk
Guest
Posts: n/a
 
      12-12-2006
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?

 
Reply With Quote
 
 
 
 
Goofy
Guest
Posts: n/a
 
      12-12-2006

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?
>



 
Reply With Quote
 
 
 
 
mike7510uk
Guest
Posts: n/a
 
      12-12-2006
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?
> >


 
Reply With Quote
 
Goofy
Guest
Posts: n/a
 
      12-12-2006
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?
>> >

>



 
Reply With Quote
 
mike7510uk
Guest
Posts: n/a
 
      12-12-2006
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?
> >> >

> >


 
Reply With Quote
 
mike7510uk
Guest
Posts: n/a
 
      12-12-2006
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: 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?
> > >> >
> > >


 
Reply With Quote
 
mike7510uk
Guest
Posts: n/a
 
      12-12-2006
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),checkb ox)


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: 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?
> > > >> >
> > > >


 
Reply With Quote
 
Jim in Arizona
Guest
Posts: n/a
 
      12-12-2006
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.
 
Reply With Quote
 
mike7510uk
Guest
Posts: n/a
 
      12-13-2006
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.


 
Reply With Quote
 
mike7510uk
Guest
Posts: n/a
 
      12-13-2006
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPNET CheckBox and a CheckBox in a TemplateField Jason Huang ASP .Net 0 06-29-2007 12:35 AM
DataGrid and embeded Checkbox..How to find if checkbox clicked =?Utf-8?B?RG90TmV0RGV2?= ASP .Net 1 10-06-2006 04:11 PM
disable checkbox list checkbox Vikram ASP .Net 1 01-25-2006 02:59 PM
Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked. Joey Pang ASP .Net Datagrid Control 4 06-13-2005 02:29 AM
Text on Checkbox below the checkbox tshad ASP .Net 0 04-14-2005 11:26 PM



Advertisments