必须声明标量变量“@ ..”gridview

时间:2014-12-24 10:39:19

标签: 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();
}

从SQLdatasource更新命令:

   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();

1 个答案:

答案 0 :(得分:0)

如果已经解决,请你改变并让我知道;

<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>