Note that there are some explanatory texts on larger screens.

plurals
  1. POVB.net - make Gridview checkbox field update boolean field in database
    primarykey
    data
    text
    <p>There are lots of questions about this but I've not been able to solve my problem using the answers to any of them (after many, many attempts..)</p> <p>I'm working in vb.net creating an asp.net web application. I have an SqlDataSource and a GridView on my page. </p> <p>I want to change the DoNotMail boolean value represented by a Gridview checkbox and automatically update in the database if the checkbox is checked from 0 (False, Will Mail) to 1 (True, Won't Mail) here is the code I used. </p> <p>For the default.aspx.vb code behind I added: </p> <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load lastname.Focus() If Page.IsPostBack Then Response.Write("The DoNotMail value has been changed in the database for the selected field") End If End Sub Public Sub checkbox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) With Me.SqlDataSource1 Dim box As CheckBox = DirectCast(sender, CheckBox) If box.Checked = True Then donotmail.SelectedValue = 1 .ConnectionString = ConfigurationManager.AppSettings("AgentLeadsConnectionString").ToString .UpdateCommand = "UPDATE MktDataLeads_scrubbed set donotmail=@donotmail" Else donotmail.SelectedValue = 0 .ConnectionString = ConfigurationManager.AppSettings("AgentLeadsConnectionString").ToString .UpdateCommand = "UPDATE MktDataLeads_scrubbed set donotmail=@donotmail" End If End With End Sub </code></pre> <p>For the default.aspx page I added: </p> <pre><code> &lt;asp:TemplateField HeaderText="DoNotMail" SortExpression="DoNotMail"&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox1_CheckedChanged" Checked='&lt;%# Bind("DoNotMail") %&gt;' Enabled="true" /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox1_CheckedChanged" Checked='&lt;%# Bind("DoNotMail") %&gt;' /&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; </code></pre> <p>1) I'm pretty sure my syntax on the update command isn't right in the default code-behind section. Does anyone know the correct syntax?<br> 2) I get the error: When I add "Handles CheckBox1.CheckedChanged" under the code behind section, CheckBox1 is underlined and gets the following error: "Handles clause requires a WithEvents variable defined in the containing type or one of its base types". How do I get rid of that? I just deleted the line and ran the code without it. 3) Upon clicking the checkbox, "The DoNotMail value has been changed in the database for the selected field" text is displayed at the top of the page but if i re-run the search for donotmail=1, the record doesn't show up becuase it was never updated w the new value. </p> <p>I'm pretty stumped. Can anyone help? It would be very much appreciated :)</p> <p>Thanks for the Response Nick! The UPDATE statement won't work b/c it has no WHERE clause. It will update the entire table as it is. There's no primary key for the table. Here are the columns for the table FROM [AgentLeads].[dbo].[MktDtaLeads_Scrubbed] - [Last Name] ,[First Name] ,[Middle Name] ,[Suffix] ,[Address Line 1] ,[Address Line 2] ,[City] ,[ST] ,[ZipCode] ,[Email Address] ,[Phone Nbr] ,[Toll Free Nbr] ,[InsertDate] ,[SentDate] ,[DoNotMail] </p> <p>Where AgentLeads is the database and MktDtaLeads_Scrubbed is the table. How do I specify the row? So would i put : </p> <p>.UpdateCommand = "UPDATE MktDataLeads_scrubbed set donotmail=@donotmail" WHERE [last name]=@lastname.selectedrow AND [first name]=@firstname.selectedrow AND [Address Line 1]=@Address Line 1.selectedrow </p> <p>is it possible to do a two way sync on the entire gridview when the user hits a button so you don't have to do an update every time a row is changed? because the user might check the box and then check another box then uncheck a box and it would be a lot of updates... </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload