Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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

C#
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);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
            Label1.Text = "Records inserted successfully";
        }   }

C#
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());

    Query.cal 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("Amount");
            dt.Columns.Add("Qty");
            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;
                    dt.Rows.Add(dr);
                }
            }
            dr = dt.NewRow();
            dr["Product Name"] = TextBox6.Text;
            dr["Amount"] = TextBox5.Text;
            dr["Qty"] = TextBox4.Text;
            dr["Product Price"] = TextBox3.Text;
            dt.Rows.Add(dr);
            grd.DataSource = dt;
            grd.DataBind();
     
    }



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

1 solution

it was my stupidity

i wrote wrong column name
thanks everyone for viewing it and appologize for ur time
regards
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900