Click here to Skip to main content
13,145,068 members (54,441 online)
Rate this:
Please Sign up or sign in to vote.
i have one form with few textboxes through which i add data into the grid using datatable and now i want to add data of grid to the table (Tbl_Invoice) with columns Invoice_Id(PK and identity specification true),Product_Name(nvarchar(50),Amount(numeric(10,2)),Product_Quantity(int),Product_Price(numeric(10,2)).

coding is

protected void Button3_Click(object sender, EventArgs e)
    {  foreach (GridViewRow g1 in grd.Rows)  //i have  first 2 columns in which i took button field EDIT and REMOVE ,so how will start my loop
        {     SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ConnectionString);
            SqlCommand com;
            com = new SqlCommand("insert into Tbl_Invoice(Product_Name,Amount,Product_Quantity,Product_Price) values ('" + g1.Cells[2].Text + "'," + g1.Cells[3].Text + "," + g1.Cells[4].Text + "," + g1.Cells[5].Text + ")", con);
            Label1.Text = "Records inserted successfully";
        }   }

public partial class invoice : System.Web.UI.Page
    DataTable dt = new DataTable();
    DataRow dr;
    public SqlDataReader reader;
    Query.product objprdt = new product(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
    Query.customer objcus = new customer(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString()); objcal = new cal(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
    DataTable dt1 = new DataTable();

  protected void Button2_Click(object sender, EventArgs e)
    {  //on click button values of text are added into grid using datatable...i have 2 first column in which i took button field EDIT and Remove
            dt.Columns.Add("Product Name");
            dt.Columns.Add("Product Price");
            for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt++)
                if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
                    dr = dt.NewRow();
                    dr["Product Name"] = grd.Rows[intCnt].Cells[2].Text;
                    dr["Amount"] = grd.Rows[intCnt].Cells[3].Text;
                    dr["Qty"] = grd.Rows[intCnt].Cells[4].Text;
                    dr["Product Price"] = grd.Rows[intCnt].Cells[5].Text;
            dr = dt.NewRow();
            dr["Product Name"] = TextBox6.Text;
            dr["Amount"] = TextBox5.Text;
            dr["Qty"] = TextBox4.Text;
            dr["Product Price"] = TextBox3.Text;
            grd.DataSource = dt;

i am getting an error "Invalid column name 'Product_Price' "on com.executenonquery
pls help
Posted 6-Feb-13 22:01pm
Updated 6-Feb-13 22:15pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

it was my stupidity

i wrote wrong column name
thanks everyone for viewing it and appologize for ur time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100