2015-06-15 46 views
0

我試圖從一個網格視圖獲取列值時,檢查行並將這些值插入到其他表, 這裏是我的C#代碼時,選擇網格視圖中的複選框 沒有給出一個或多個必需參數的值。在asp.net

protected void chkfun_CheckedChanged(object sender, EventArgs e) 
     { 
      CheckBox chkstatus = (CheckBox)sender; 
      GridViewRow row = (GridViewRow)chkstatus.NamingContainer; 
     } 
    protected void hidebutton_Click(object sender, EventArgs e) 
     { 
      OleDbConnection acc = dc_object.a_object.Connect("CARLI_funds_FY16"); 
      OleDbCommand commd = new OleDbCommand(); 
      commd.Connection = acc; 
      CheckBox x1; 
      HiddenField x2; 
      for (int i = 0; i < GridView1.Rows.Count; i++) 
      { 
       x1 = GridView1.Rows[i].FindControl("chkfun") as CheckBox; 
       if (x1.Checked) 
       { 
        x2 = GridView1.Rows[i].FindControl("hdnfld") as HiddenField; 
        var fund_code = (GridView1.Rows[i].FindControl("lblfundcode") as Label).Text; 
        var fund_name = (GridView1.Rows[i].FindControl("lblfundname") as Label).Text; 

        commd.CommandText = "insert into funds_hidden values(fund_code,fund_name)"; 
       commd.ExecuteNonQuery(); 
       } 

      } 
      acc.Close(); 
      Response.Redirect("allfunds.aspx"); 
     } 

這裏是我的aspx代碼

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID,FUND_CODE,FUND_NAME" AllowSorting="True" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"> 
       <AlternatingRowStyle BackColor="White" /> 
       <Columns> 
        <asp:TemplateField> 
         <HeaderTemplate> 
          <asp:CheckBox ID="chkheader" runat="server" OnCheckedChanged="chkheader_CheckedChanged" AutoPostBack="True" /> 
         </HeaderTemplate> 
         <ItemTemplate> 
          <asp:CheckBox ID="chkfun" runat="server" OnCheckedChanged="chkfun_CheckedChanged" AutoPostBack="True" /> 
          <asp:HiddenField ID="hdnfld" runat="server" value='<%# Eval("ID") %>'/> 
         </ItemTemplate> 
        </asp:TemplateField> 
          <asp:TemplateField HeaderText="FUND_CODE"> 
         <ItemTemplate> 
         <asp:Label ID="lblfundcode" runat="server" Text='<%# Eval("FUND_CODE") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="FUND_NAME"> 
         <ItemTemplate>`enter code here` 
         <asp:Label ID="lblfundname" runat="server" Text='<%# Eval("FUND_NAME") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
       </Columns> 
       </asp:GridView> 
       <asp:LinkButton ID="hidebutton" runat="server" OnClick="hidebutton_Click" Text="hide" /> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" SortParameterName="" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT ID,NORMAL_FUND_NAME AS FUND_NAME, NORMAL_FUND_CODE AS FUND_CODE FROM funds_sample1 WHERE (LEDGER_ID = 43) AND (PARENT_FUND = 1124 OR PARENT_FUND = 984 OR PARENT_FUND = 370 OR PARENT_FUND = 363)"></asp:SqlDataSource> 
     </form> 

我只是初學者請指導我,如果有在我的代碼和世界衛生大會的任何錯誤T應當做些什麼來清除此錯誤,在此先感謝

+0

你需要查找如何做一個參數化的查詢具體查找'OleDbCommand.Parameters'在msdn C#上的例子,在那裏以及在互聯網上的大量工作示例..錯誤消息明確指出什麼是問題..你需要提供參數名稱加上2參數的值,你可能需要一個where子句以及'commd.CommandT ext =「插入funds_hidden值(fund_code,fund_name)」;' – MethodMan

回答

1

你應該爲你的價值被指定參數:

commd.CommandText = "insert into funds_hidden values(?, ?)"; 
commd.Parameters.Add("code", OleDbType.VarChar).Value = fund_code; 
commd.Parameters.Add("name", OleDbType.VarChar).Value = fund_name; 

我也建議:

  • 學習一下C#命名約定(使用camelCase而不是underscore_separators)
  • 對數據庫資源(如命令和連接)使用using語句
  • 指定要在SQL語句更新
+0

是的非常感謝你@Jon,這是完美的作品,我也會了解它們 – jay

0

我已經改變了我的INSERT語句如下方式字段名稱,使其工作,感謝幫助其現在的工作

for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 
      x1 = GridView1.Rows[i].FindControl("chkfun") as CheckBox; 
      if (x1.Checked) 
      { 
       x2 = GridView1.Rows[i].FindControl("hdnfld") as HiddenField; 
       var fund_code = (GridView1.Rows[i].FindControl("lblfundcode") as Label).Text; 
       var fund_name = (GridView1.Rows[i].FindControl("lblfundname") as Label).Text; 

       string insertstatement = "insert into funds_hidden(fund_code,fund_name)"+ " values (?,?)" ; 

       OleDbCommand insertcommand = new OleDbCommand(insertstatement, acc); 
       insertcommand.Parameters.Add("fund_code", OleDbType.Char).Value = fund_code; 
       insertcommand.Parameters.Add("fund_name", OleDbType.Char).Value = fund_name; 
insertcommand.ExecuteNonQuery(); 
相關問題