Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have stored some record in session variable and displaying these records in gridview with datatable but when I submit these records to save only last record saved.

suggest me, my code is..

when I use add button for datatable is

C#
protected void btnAdd_Click(object sender, EventArgs e)
       {
           double a, b;
           a = double.Parse(txtqty.Text);
           b = a * double.Parse(LblRate1.Text);
           txtAmount.Text = b.ToString();

              dt = (DataTable)Session["Address"];
               dr = dt.NewRow();

               dr["Product_Name"] = DropDownList1.SelectedItem.Text;
               dr["Product_Quantity"] = txtqty.Text;
               dr["product_rate"] = LblRate1.Text;
               dr["Product_Amount"] = txtAmount.Text;
               dt.Rows.Add(dr);
               dt.AcceptChanges();
               GridView1.DataSource = dt;
               GridView1.DataBind();
               GridView1.Visible = true;

       }


and when submit button for database is

C#
protected void btnSubmit_Click(object sender, EventArgs e)
        {
           grandtotal();
           insertproductid();
            try
            {
                con.Open();
                cmd = new SqlCommand("insert_order", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@customer_name", SqlDbType.NVarChar).Value = txtName.Text;
                cmd.Parameters.Add("@customer_phone", SqlDbType.NVarChar).Value = TxtPhone.Text;
                cmd.Parameters.Add("@customer_address", SqlDbType.NVarChar).Value = txtAddress.Text;
                cmd.Parameters.Add("@product_total_amount", SqlDbType.Int).Value =txtTotalAmount.Text;
                cmd.Parameters.Add("@product_quantity", SqlDbType.Int).Value =txtqty.Text;
                cmd.Parameters.Add("@product_amount", SqlDbType.Int).Value = txtAmount.Text;
                cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
            }


what will be change in this code??
Posted
Updated 23-Sep-10 20:40pm
v2

You are inserting only the last record into the database.

Run a loop for all your new records and call the stored procedure for each new entry.
 
Share this answer
 
Comments
call to .net 24-Sep-10 3:04am    
but on add button i am displaying all records in datatable these records are not saving database on add button while i insert all entry then click on submit button.
If you are trying to insert all the records that you have in your datatable than iterate through those records while calling the stored procedure . In your submit event you are inserting values from your textboxes which will be the last one you have on your web page when you hit the submit button.


C#
for (int i = 0; i < GridView1.Rows.Count; i++)
            {
string Name =GridView1.Rows[i].Cells[0].Text.Trim(); 
.... // Rest of the code  like that
....

InsertMyRecord(Name,...//Other Parameters);

}


Put your SQL code in a separate procedure and pass values to it(This is not essential but just a recommendation to make your code bit elegant).
 
Share this answer
 
Have a look Insert multiple rows[^]
 
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