am trying to get the column values from a grid view when a row is checked and insert those values into some other table , here is my c # code when a check box in grid view is selected
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");
}
and here is my code for 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>
am just beginner please guide me if there are any mistakes in my code and what should i do to clear this error, thanks in advance
You should be specifying parameters for your values:
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;
I'd also recommend:
using
statement for database resources (such as the command and connection)See more on this question at Stackoverflow