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..)
I'm working in vb.net creating an asp.net web application. I have an SqlDataSource and a GridView on my page.
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.
For the default.aspx.vb code behind I added:
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
For the default.aspx page I added:
<asp:TemplateField HeaderText="DoNotMail" SortExpression="DoNotMail">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox1_CheckedChanged" Checked='<%# Bind("DoNotMail") %>'
Enabled="true" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox1_CheckedChanged" Checked='<%# Bind("DoNotMail") %>' />
</EditItemTemplate>
</asp:TemplateField>
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?
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.
I'm pretty stumped. Can anyone help? It would be very much appreciated :)
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]
Where AgentLeads is the database and MktDtaLeads_Scrubbed is the table. How do I specify the row? So would i put :
.UpdateCommand = "UPDATE MktDataLeads_scrubbed set donotmail=@donotmail" WHERE [last name][email protected] AND [first name][email protected] AND [Address Line 1]=@Address Line 1.selectedrow
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...