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
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]);
}
}
}
}
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.