c# - 必须声明标量变量 "@.."gridview

标签 c# asp.net gridview

我试图更新我的 gridview 中的一行,但不是整行只是名字和姓氏值

但是我得到了上面的错误,我从后面的代码中添加了我的值,它们都被定义了。该错误发生在运行时在控制台中

这是我的代码

代码隐藏:

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            TextBox updateForeName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtForename");

            TextBox updateSurName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSurname");


          //  string contactID = GridView1.DataKeys[e.RowIndex].Value.ToString();

            string contactID = GridView1.DataKeys[e.RowIndex].Value.ToString();

           SqlDataSource1.UpdateParameters["ContactID"].DefaultValue = contactID;

            SqlDataSource1.UpdateParameters["Forename"].DefaultValue = updateForeName.Text;
            SqlDataSource1.UpdateParameters["Surname"].DefaultValue = updateSurName.Text;
            SqlDataSource1.Update();
}

从 SQL 数据源更新命令:

   UPDATE tblcontact
SET          Forename = @Forename, Surname = @Surname
WHERE  (ContactID = @ContactID)

这是 gridview 标记的相关片段

  <asp:GridView ID="GridView1" CssClass="table table-hover table-condensed targetFont" runat="server" DataSourceID="SqlDataSource1" RowStyle-Wrap="true" AutoGenerateColumns="False" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowUpdating="GridView1_RowUpdating"
                                DataKeyNames="ContactID" EmptyDataText="There is donation data to be displayed" OnLoad="GridView1_Load" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
                                <Columns>
                                    <asp:CommandField ShowSelectButton="True" ShowEditButton="True" />
                                    <asp:BoundField DataField="ContactID" HeaderText="ContactID" SortExpression="ContactID" ReadOnly="false" Visible="false"></asp:BoundField>
                                    <asp:TemplateField HeaderText="Forename" SortExpression="Forename">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtForename" runat="server" Text='<%# Bind("Forename") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblForename" runat="server" Text='<%# Bind("Forename") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Surname" SortExpression="Surname">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtSurname" runat="server" Text='<%# Bind("Surname") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblSurename" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="Business Name" HeaderText="Business Name" Visible="False" ></asp:BoundField>

                                    <asp:BoundField DataField="House Number" HeaderText="House Number" />
                                    <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" ></asp:BoundField>
                                    <asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" ReadOnly="True" Visible="False"></asp:BoundField>

SqlDataSource1 标记

<asp:SqlDataSource ID="SqlDataSource1" runat="server" OnSelected="SqlDataSource1_Selected" OnSelecting="SqlDataSource1_Selecting" ConnectionString="<%$ ConnectionStrings:AreaCollectionConnectionString %>"
        SelectCommand="SELECT DISTINCT tblcontact.ContactID, tblcontact.Forename, tblcontact.Surname, tbladdress.[House Number], tbladdress.AddressLine1, tbladdress.AddressLine2, tblcontact.[Business Name] FROM tblcontact INNER JOIN tbladdress ON tblcontact.AddressID = tbladdress.AddressID LEFT OUTER JOIN tblDonate ON tblcontact.ContactID = tblDonate.ContactID WHERE (tbladdress.CollectionArea = @CollectionArea) AND (tbladdress.AddressLine1 = @drpCollectionStreet) ORDER BY tbladdress.AddressLine1"
        InsertCommand="INSERT INTO tblDonate(DonationMonth, NoDonationReason, ContactID, DonationAmount, Date) VALUES (@DonationMonth, @NoDonationReason, @ContactID, @DonationAmount, @Date)" 
        UpdateCommand="UPDATE tblcontact SET Forename = @Forename, Surname = @Surname WHERE (ContactID = @ContactID)">
        <InsertParameters>
            <asp:Parameter Name="DonationMonth" />
            <asp:Parameter Name="NoDonationReason" />
            <asp:Parameter Name="ContactID" />
            <asp:Parameter Name="DonationAmount" />
            <asp:Parameter Name="Date" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="drpCollectionAreaSelection" Name="CollectionArea" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="drpCollectionStreet" Name="drpCollectionStreet" PropertyName="SelectedValue" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Forename" />
            <asp:Parameter Name="Surname" />
            <asp:Parameter Name="ContactID" />


        </UpdateParameters>
    </asp:SqlDataSource>

我在其他地方使用下面的,它工作正常

string donationMonth = (GridView2.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList).SelectedItem.Value;
            string donationID = GridView2.DataKeys[e.RowIndex].Value.ToString();
            string DonationAmount = (GridView2.Rows[e.RowIndex].FindControl("txtDonationAmount") as TextBox).Text;


            SqlDataSource2.UpdateParameters["DonationMonth"].DefaultValue = donationMonth;
            SqlDataSource2.UpdateParameters["DonationAmount"].DefaultValue = DonationAmount;
            SqlDataSource2.Update();

最佳答案

请您更改一下,如果解决了请告诉我;

<asp:BoundField DataField="ContactID" HeaderText="ContactID" SortExpression="ContactID" ReadOnly="false" Visible="false"></asp:BoundField>

<asp:BoundField DataField="ContactID" HeaderText="ContactID" SortExpression="ContactID" **ReadOnly="true"** Visible="false"></asp:BoundField>

关于c# - 必须声明标量变量 "@.."gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635557/

相关文章:

c# - 什么是 "index out of range"异常,如何修复它?

c# - 具有相同返回类型和名称的 MessageBodyMembers 导致异常 - 元素已导出

c# - Mondor 的 MSCaptcha 在部署到托管服务时不会加载图像

asp.net - 尝试向服务器发送 byte[] 时,Xamarin 中出现 "Cannot access a disposedObject"异常

c# - 如何获取 Gridview 底层数据源?

android - 如何让 GridView 在其 LinearLayout 父级中居中?

c# - ASP.NET MVC 5 : App Pool, Windows 身份验证和 Active Directory

c# - 无法以编程方式加载 xll

javascript - 将 jquery.slideToggle() 与 Knockout JS 结合使用

html - 我如何在媒体查询中隐藏 HTML 元素?