0

1) 对于带有复选框的网格视图,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上进行双向同步,这样您就不必在每次更改行时都进行更新?因为用户可能会选中该框,然后选中另一个框,然后取消选中一个框,这将是很多更新...

4

2 回答 2

0

这是一个艰难的情况,因为您无法区分没有唯一主键的相同记录。

但是,您可以做的是利用GridView 的 OldValues 属性。这不是开发人员通常处理 GridViews 的方式,但我认为它可能只是你的救星。

例如,要确定要更新哪一行,您可以将where子句设置为每个属性的所有 OldValues,并相应地更新。由于您的棘手情况,您最终可能只更新多行 - 但这是您为没有主键付出的代价。

于 2011-02-08T15:47:08.470 回答
0

这是 default.aspx 上 GridView 的代码:

我使用了此页面上详述的代码的变体并让它工作!vb.net SQL 查询在 SQL 服务器中有效,但在从复选框调用时无效

对于我添加的 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>
于 2011-02-08T21:21:51.060 回答