VB.NET - Gridview 中没有主键的表的 SQL UpdateCommand
1) 对于带有复选框的 gridview,在 vb.net 中没有主键的表的更新查询的语法是什么?
免责声明:令人沮丧的是,添加主键不是一个选项。我的程序是一个大得多的系统中的一个小程序,数据管理很差。我的开发时间不包括重写其他软件。
以下是表的列,其中 AgentLeads 是数据库,MktDtaLeads_Scrubbed 是表:
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]
我现在拥有的代码不显示任何错误,但当您选中复选框时不会更新 DoNotMail 字段,即使它确实显示文本“数据库中所选字段的 DoNotMail 值已更改”。
对于后面的 default.aspx.vb 代码,我添加了:
Public Sub gridview1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "UpdateDoNotMail" Then
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
WHERE [last name][email protected] AND [first name][email protected] AND [Address Line 1]=@Address Line 1.selectedrow"
Else
donotmail.SelectedValue = 0
.ConnectionString = ConfigurationManager.AppSettings("AgentLeadsConnectionString").ToString
.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"
End If
End With
End If
End Sub
这是 default.aspx 上 GridView 的代码:
<asp:GridView ID="GridView2" runat="server" CellPadding="2"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Last Name" HeaderText="Last Name"
SortExpression="Last Name" />
<asp:BoundField DataField="First Name" HeaderText="First Name"
SortExpression="First Name" />
<asp:BoundField DataField="Address Line 1" HeaderText="Addr 1"
SortExpression="Address Line 1" />
<asp:BoundField DataField="Address Line 2" HeaderText="Addr 2"
SortExpression="Address Line 2" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="ST" HeaderText="ST" SortExpression="ST" />
<asp:BoundField DataField="ZipCode" HeaderText="ZipCode"
SortExpression="ZipCode" />
<asp:BoundField DataField="Email Address" HeaderText="Email Addr"
SortExpression="Email Address" />
<asp:BoundField DataField="Phone Nbr" HeaderText="Phone Nbr"
SortExpression="Phone Nbr" />
<asp:TemplateField HeaderText="DoNotMail" SortExpression="DoNotMail">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" CommandName="UpdateDoNotMail" Checked='<%# Bind("DoNotMail") %>'
Enabled="true" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" CommandName="UpdateDoNotMail" Checked='<%# Bind("DoNotMail") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
2)当用户点击按钮时,是否可以在整个 gridview 上进行双向同步,这样您就不需要每次更改行时都进行更新?因为用户可能会选中该框,然后选中另一个框,然后取消选中一个框,这将是很多更新......
1) What's the syntax for a Update query for a table without a primary key in vb.net for a gridview with a checkbox?
Disclaimer: Frustratingly, adding a primary key is not an option. My program is a small program in a much larger system with poor data management. My development time does not include rewriting the other software.
Here are the Columns for the table Where AgentLeads is the database and MktDtaLeads_Scrubbed is 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]
The code I have right now doesn't display any errors but doesn't update the DoNotMail field when you check the checkbox even though it does display the text "The DoNotMail value has been changed in the database for the selected field".
For the default.aspx.vb code behind I added:
Public Sub gridview1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "UpdateDoNotMail" Then
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
WHERE [last name][email protected] AND [first name][email protected] AND [Address Line 1]=@Address Line 1.selectedrow"
Else
donotmail.SelectedValue = 0
.ConnectionString = ConfigurationManager.AppSettings("AgentLeadsConnectionString").ToString
.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"
End If
End With
End If
End Sub
Here's the code for the GridView on default.aspx:
<asp:GridView ID="GridView2" runat="server" CellPadding="2"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Last Name" HeaderText="Last Name"
SortExpression="Last Name" />
<asp:BoundField DataField="First Name" HeaderText="First Name"
SortExpression="First Name" />
<asp:BoundField DataField="Address Line 1" HeaderText="Addr 1"
SortExpression="Address Line 1" />
<asp:BoundField DataField="Address Line 2" HeaderText="Addr 2"
SortExpression="Address Line 2" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="ST" HeaderText="ST" SortExpression="ST" />
<asp:BoundField DataField="ZipCode" HeaderText="ZipCode"
SortExpression="ZipCode" />
<asp:BoundField DataField="Email Address" HeaderText="Email Addr"
SortExpression="Email Address" />
<asp:BoundField DataField="Phone Nbr" HeaderText="Phone Nbr"
SortExpression="Phone Nbr" />
<asp:TemplateField HeaderText="DoNotMail" SortExpression="DoNotMail">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" CommandName="UpdateDoNotMail" Checked='<%# Bind("DoNotMail") %>'
Enabled="true" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" CommandName="UpdateDoNotMail" Checked='<%# Bind("DoNotMail") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
2) 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个困难的情况,因为您无法区分没有唯一主键的相同记录。
但是,您可以做的是利用 GridView 的 OldValues 属性。这不是开发人员通常处理 GridView 的方式,但我认为它可能是您的救星。
例如,要确定要更新哪一行,您可以将
where
子句设置为每个属性的所有 OldValues,并进行相应更新。由于您的棘手情况,您可能最终会更新不止一行 - 但这是您为没有主键而付出的代价。This is a tough situation to be in because you can't distinguish between records that are identical without a unique primary key.
What you can do, however, is to make use of GridView's OldValues property. This isn't how developers usually handle GridViews but I think it may just be your savior here.
For example, to figure out which row to update, you'd set your
where
clause to all the OldValues of every property, and update accordingly. Because of your sticky situation, you may just end up updating more than one row - but that's the price you pay for no primary key.以下是 default.aspx 上 GridView 的代码:
我使用了此页面上详细介绍的代码的变体,并使其正常工作! vb.net SQL查询在 SQL Server 中有效,但在从复选框调用时无效
对于我添加的 default.aspx.vb 代码:
对于 gridview,我使用了相同的代码:
Here's the code for the GridView on default.aspx:
I used a variation of the code detailed on this page and got it to work! vb.net SQL query works in SQL server but not when called from checkbox
For the default.aspx.vb code behind I added:
For the gridview I used the same code: