Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to get the all the data from one table and stored that in dataset and have to insert those values in another table. I have selected values using datakeynames of gridview there are more records in CartGridview but it takes only one record that is latest value How to get the all records from gridview using datakeynames??
My Code as follows
C#
protected void Gridcart_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
       foreach (GridViewRow row in Gridcart.Rows)
       {
           {
               Application["id"] = Convert.ToInt32(Gridcart.DataKeys[e.Row.RowIndex].Values[0]);
           }

       }
   }
}

C#
protected void orderimgbtn_Click(object sender, ImageClickEventArgs e)
{
   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[z][0]);
                   command.Parameters.AddWithValue("@productname", ds.Tables[0].Rows[z][1]);
                   command.Parameters.AddWithValue("@ProductImage", ds.Tables[0].Rows[z][2]);
                   command.Parameters.AddWithValue("@price", ds.Tables[0].Rows[z][3]);
                   command.Parameters.AddWithValue("@qty", ds.Tables[0].Rows[z][4]);
                   command.Parameters.AddWithValue("@totalcost", ds.Tables[0].Rows[z][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();
           }
       }
   }

While Inserting values in rsa_Orderconfirmation getting error like There is no row at position 1. Please guide me to solve this.
Posted
Updated 22-Jun-15 23:55pm
v4
Comments
Richard Deeming 22-Jun-15 8:26am    
The select part of your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Suvendu Shekhar Giri 22-Jun-15 23:40pm    
Instead of accessing values from datatable row using index, better to get them by name like
ds.Tables[0].Rows[z]["colName"]);

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