Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one table for cart have to insert those values into another table but its takes only the last value
my code as follows
C#
if (Convert.ToDecimal(Label1.Text) > 0)
       {
         if (Convert.ToDecimal(Label1.Text) > 0)
        {
            string proid = Application["id"].ToString();
            con.Open();
            da = new SqlDataAdapter("select tpd.productid,tpd.productname,tpd.ProductImage,tpd.price,tpd.qty,tpd.totalcost,tpd.cdate from rsa_addtocart tpd where tpd.productid=" + proid + " ", con);
            ds = new DataSet();
            da.Fill(ds, "tbl_tpd");
            if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
           {

               using (var command = new SqlCommand("insert into rsa_Orderconfirmation(UserId, productid, productname, ProductImage, price, qty, totalcost, cdate) values (@UserId, @productid, @productname, @ProductImage, @price, @qty, @totalcost, @cdate)", con))
               {
                   for (int z = 0; z < this.Gridcart.Rows.Count; z++)
                   {
                       command.Parameters.Clear();
                       command.Parameters.AddWithValue("@UserId", Session["users"]);
                       command.Parameters.AddWithValue("@productid", ds.Tables[0].Rows[0][0]);
                       command.Parameters.AddWithValue("@productname", ds.Tables[0].Rows[0][1]);
                       command.Parameters.AddWithValue("@ProductImage", ds.Tables[0].Rows[0][2]);
                       command.Parameters.AddWithValue("@price", ds.Tables[0].Rows[0][3]);
                       command.Parameters.AddWithValue("@qty", ds.Tables[0].Rows[0][4]);
                       command.Parameters.AddWithValue("@totalcost", ds.Tables[0].Rows[0][5]);
                       command.Parameters.AddWithValue("@cdate", DateTime.Now);

                       if (con.State != ConnectionState.Open)
                       {
                           con.Open();
                           try
                           {
                               command.ExecuteNonQuery();
                           }
                           finally
                           {
                               con.Close();
                           }
                       }
                       else
                       {
                           command.ExecuteNonQuery();
                       }
                   }

                       Response.Redirect("~/BillingDetails.aspx");
                       con.Close();
                   }
            }
            }

only its inserts the last value i want to insert what are the values in cart table
how it can be done someone can help me?
Posted
Updated 17-Jun-15 0:02am
v2
Comments
Sreekanth Mothukuru 15-Jun-15 10:54am    
Try to use Table valued parameters in Sql server stored procedure to insert bulk records in a single shot.

Check the link below:
http://www.codeproject.com/Articles/39161/C-and-Table-Value-Parameters
sasanka sekhar panda 17-Jun-15 7:28am    
Agreed.... we can use table valued parameter in the sp for the above type of scenario..
Its bad to open and close db connection for a single record..

You are using Rows[0] in stead of Rows[z]. You are not iterating the data but setting the same each time...
and....
you are fisnished looping the for before sending any data to the db. No wonder the result is disappointing ;)
 
Share this answer
 
v2
Comments
kwelpooh 15-Jun-15 13:57pm    
how to clear that for loop. please
Herman<T>.Instance 15-Jun-15 16:01pm    
move the } at the end of the for loop just before the } of the using end part.
kwelpooh 16-Jun-15 9:16am    
i did that already but it adds the same item 2 times
Herman<T>.Instance 16-Jun-15 9:51am    
Can you update the code in the above question? I can then see what you have done. I you have double rows, it is possible your query SELECT returns 2 rows. Have you checked that with a debug?
Herman<T>.Instance 17-Jun-15 10:15am    
You still have rows[0] in your source in stead of rows[z]
Directly pass DataTable in stored procedure.
You can use UserDefine Table (UDT) in SQL and directly use query
condition is UDT must have same column as DataTable have which you are passing to Sql procedure.

Ex. in procedure

Insert into TableName select * from UDT
 
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