我試圖從一個網格視圖獲取列值時,檢查行並將這些值插入到其他表, 這裏是我的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應當做些什麼來清除此錯誤,在此先感謝
你需要查找如何做一個參數化的查詢具體查找'OleDbCommand.Parameters'在msdn C#上的例子,在那裏以及在互聯網上的大量工作示例..錯誤消息明確指出什麼是問題..你需要提供參數名稱加上2參數的值,你可能需要一個where子句以及'commd.CommandT ext =「插入funds_hidden值(fund_code,fund_name)」;' – MethodMan