2016-07-18 185 views
0

我有一個具有標籤和3個複選框的編輯按鈕的GridView,但是當我嘗試編輯我的GridView中的一行時,出現此錯誤。 System.Data.SqlClient.SqlException:必須聲明標量變量「@ chkBeg1」。 這裏是我的代碼:必須聲明標量變量「@ chkBeg1」

<asp:GridView class="gridview" ID="GridView1" runat="server" AutoGenerateColumns="false" 
     DataKeyNames="id" SkinID="Professional" Font-Name="Verdana" 
       Font-Size="10pt" Cellpadding="4" 
      HeaderStyle-BackColor="#109a50" 
      HeaderStyle-ForeColor="White" 
       AlternatingRowStyle-BackColor="#dddddd" DataSourceID="SqlDataSource1" > 
      <Columns> 

       <asp:BoundField DataField="language" HeaderText="language" 
        SortExpression="language" /> 
       <asp:TemplateField HeaderText="Beginner"> 
       <ItemTemplate> 
        <asp:CheckBox ID="chkBeg" runat="server" Checked ='<%# GetStatus(Eval("Beginner")) %>' Enabled="False" onclick="CheckBoxCheck(this);" /> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:CheckBox ID="chkBeg1" runat="server" Checked='<%# GetStatus(Eval("Beginner")) %>' Enabled="True" onclick="CheckBoxCheck(this);" /> 
       </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Intermediate"> 
       <ItemTemplate> 
        <asp:CheckBox ID="chkIn" runat="server" Checked ='<%# GetStatus(Eval("Intermediate")) %>' Enabled="False" onclick="CheckBoxCheck(this);" /> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:CheckBox ID="chkIn1" runat="server" Checked='<%# GetStatus(Eval("Intermediate")) %>' Enabled="True" onclick="CheckBoxCheck(this);" /> 
       </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Advance"> 
       <ItemTemplate> 
        <asp:CheckBox ID="chkAd" runat="server" Checked ='<%# GetStatus(Eval("Advance")) %>' Enabled="False" onclick="CheckBoxCheck(this);" /> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:CheckBox ID="chkAd1" runat="server" Checked='<%# GetStatus(Eval("Advance")) %>' Enabled="True" onclick="CheckBoxCheck(this);" /> 
       </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:CommandField ShowEditButton="True" /> 
</Columns> 
     </asp:GridView> 

下面是我的編輯按鈕的代碼:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="Data Source=db2.sescor.net;Initial Catalog=SDMS_PORTAL;User ID=sdmsusr;[email protected]" 
     ProviderName="System.Data.SqlClient" 
     SelectCommand="SELECT id, Idno, language, beginner, intermediate, advance FROM [sample_skills]" FilterExpression="Idno = '{0}'" 
     UpdateCommand="UPDATE [sample_skills] SET 
         [beginner] = @chkBeg1, [intermediate] = @chkIn1,[advance] = @chkAd1 
         WHERE [id] = @id"> 
     <FilterParameters> 
     <asp:ControlParameter Name="skills" ControlID="label9" PropertyName="Text" /> 
     </FilterParameters> 
    </asp:SqlDataSource> 

感謝。

回答

1

您必須添加的SqlDataSource UpdateParameters像這樣:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="Data Source=db2.sescor.net;Initial Catalog=SDMS_PORTAL;User ID=sdmsusr;[email protected]" 
     ProviderName="System.Data.SqlClient" 
     SelectCommand="SELECT id, Idno, language, beginner, intermediate, advance FROM [sample_skills]" FilterExpression="Idno = '{0}'" 
     UpdateCommand="UPDATE [sample_skills] SET 
         [beginner] = @chkBeg1, [intermediate] = @chkIn1,[advance] = @chkAd1 
         WHERE [id] = @id"> 

      <UpdateParameters> 
       <asp:ControlParameter Name="chkBeg1" ControlId="CheckBox1" PropertyName="Checked"/> 
      </UpdateParameters> 


     <FilterParameters> 
     <asp:ControlParameter Name="skills" ControlID="label9" PropertyName="Text" /> 
     </FilterParameters> 
    </asp:SqlDataSource> 

有關的SqlDataSource UpdateParameters更多信息,請點擊此鏈接:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updateparameters(v=vs.110).aspx

+0

可以請你添加更多細節.. – Emz1402

+0

爲什麼它的屬性名是「文本」..它只能包含True或False,因爲它是一個複選框。 – Emz1402

+0

ok,再次檢查答案.i更新。