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]=@lastname.selectedrow AND [first name]=@firstname.selectedrow 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]=@lastname.selectedrow AND [first name]=@firstname.selectedrow 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上进行双向同步,这样每次更改行时都不必进行更新?因为用户可能会选中该框,然后选中另一个框,然后取消选中一个框,这将是很多更新......
答案 0 :(得分:0)
这是一个艰难的情况,因为你无法区分没有唯一主键的相同记录。
然而,您可以做的是使用GridView's OldValues property。这不是开发人员通常处理GridViews的方式,但我认为它可能只是你的救星。
例如,要确定要更新的行,您需要将where
子句设置为每个属性的所有OldValues,并相应地更新。由于您的情况很糟糕,您可能最终会更新多行 - 但这是您为没有主键而支付的价格。
答案 1 :(得分:0)
以下是default.aspx上的GridView代码:
我使用了本页详细介绍的代码变体,并将其运行起来! vb.net SQL query works in SQL server but not when called from checkbox
对于我添加的default.aspx.vb代码:
Public Sub checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) 'Handles checkbox.CheckedChanged
Dim connectionString As String = ConfigurationManager.ConnectionStrings("AgentLeadsConnectionString").ConnectionString
Dim box As CheckBox = DirectCast(sender, CheckBox)
Dim tblcell As TableCell = CType(box.Parent, TableCell)
Dim dgRow As GridViewRow = CType(tblcell.Parent, GridViewRow)
Dim lastname As String = [last name].Rows(dgRow.DataItemIndex).Cells(0).Text
Dim firstname As String = [first name].Rows(dgRow.DataItemIndex).Cells(0).Text
Dim address As String = [Address Line1].Rows(dgRow.DataItemIndex).Cells(0).Text
Dim insertSQL As String
If box.Checked = True Then
insertSQL = "UPDATE MktDataLeads_scrubbed "
insertSQL &= "SET donotmail=1 "
insertSQL &= "WHERE [last name]= @lastname AND [first name]=@firstname AND [Address Line1]=@address "
Else
insertSQL = "UPDATE MktDataLeads_scrubbed "
insertSQL &= "SET donotmail=0 "
insertSQL &= "WHERE [last name]= @lastname AND [first name]=@firstname AND [Address Line1]=@address "
End If
Using con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(insertSQL, con)
cmd.Parameters.AddWithValue("@donotmail", donotmail)
Try
con.Open()
cmd.ExecuteNonQuery()
Catch Err As SqlException
MsgBox("Error", 65584, "Insertion Error")
End Try
con.Close()
End Using
End Sub
对于gridview,我使用了相同的代码:
<asp:TemplateField HeaderText="DoNotMail" SortExpression="DoNotMail">
<ItemTemplate>
<asp:CheckBox ID="CheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox_CheckedChanged" Checked='<%# Bind("DoNotMail") %>'
Enabled="true" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="checkbox_CheckedChanged" Checked='<%# Bind("DoNotMail") %>' />
</EditItemTemplate>
</asp:TemplateField>