在gridview中单击编辑时出错

时间:2012-11-08 17:27:13

标签: c# asp.net gridview checkbox edit

错误讯息:

  

已声明变量名'@Bitnet'。变量名在查询批处理或存储过程中必须是唯一的。在尝试编辑gridview时。

我希望能够更新复选框字段。此gridview基于表连接。

<asp:Content ID="Content4" runat="server" contentplaceholderid="ContentPlaceHolder2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"   DataKeyNames="INST_ID,BITNET" DataSourceID="SqlDataSource1" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="INST_ID" HeaderText="INST_ID" ReadOnly="True" SortExpression="INST_ID" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
        <asp:BoundField DataField="Prefix" HeaderText="Prefix" SortExpression="Prefix" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" />
        <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
        <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
        <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
        <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
        <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
        <asp:BoundField DataField="BITNET" HeaderText="BITNET" ReadOnly="True" SortExpression="BITNET" />
        <asp:TemplateField HeaderText="Active">
            <ItemTemplate>
                <asp:CheckBox ID="Active" runat="server" Checked='<%# Eval("Active").ToString() == "1" ? true:false %>' Enabled="false" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="Active" runat="server" Checked="true" Enabled="false" />
            </EditItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = @Bitnet)">
     </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:1)

您必须先声明参数。然后只有你可以使用它。它应该是这样的

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = @Bitnet)">
<SelectParameters>
    <asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</SelectParameters>
<UpdateParameters>
    <asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</UpdateParameters>
             </asp:SqlDataSource>

答案 1 :(得分:1)

我解决了自己的问题是我没有先清除参数               protected void Page_Load(object sender,EventArgs e)         {             SqlDataSource1.SelectParameters.Clear();         }

修复了这个问题,现在工作正常。