必须声明标量变量“@.网格视图”
本文关键字:网格 视图 变量 声明 标量 | 更新日期: 2023-09-27 18:35:15
我试图更新我的网格视图中的一行,但不是整行,只是名字和姓氏值
但是我收到上述错误,我正在从代码后面添加我的值,并且它们都是定义的。 在控制台中运行时发生错误
这是我的代码
代码隐藏:
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)
这是网格视图标记的相关截图
<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>