C# 获取gridview中每行的Datakeynames值
我想从一个表中获取所有数据并将其存储在dataset中,并且必须在另一个表中插入这些值。我选择了使用gridview的datakeynames的值CartGridview中有更多记录,但只需要一条记录是最新的值如何使用datakeynames从gridview获取所有记录??它只插入最新的值C# 获取gridview中每行的Datakeynames值,c#,asp.net,C#,Asp.net,我想从一个表中获取所有数据并将其存储在dataset中,并且必须在另一个表中插入这些值。我选择了使用gridview的datakeynames的值CartGridview中有更多记录,但只需要一条记录是最新的值如何使用datakeynames从gridview获取所有记录??它只插入最新的值 protected void Gridcart_RowDataBound(object sender, GridViewRowEventArgs e) { foreach (Grid
protected void Gridcart_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow gvrow in Gridcart.Rows)
{
string ID = Gridcart.DataKeys[gvrow.RowIndex].Values[0].ToString();
Application["Id"] = ID;
}
}
protected void orderimgbtn_Click(object sender, ImageClickEventArgs e)
{
try
{
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_tp");
if (ds.Tables.Count > 0 && ds.Tables["tbl_tp"].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();
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}
else
{
command.ExecuteNonQuery();
}
}
Response.Redirect("~/BillingDetails.aspx");
con.Close();
}
}
}
}
catch (Exception ex)
{
}
}
protectedvoid Gridcart\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
foreach(Gridcart.Rows中的GridViewRow gvrow)
{
字符串ID=Gridcart.DataKeys[gvrow.RowIndex]。值[0]。ToString();
应用程序[“Id”]=Id;
}
}
受保护的无效订单imgbtn\u单击(对象发送方,图像单击事件参数e)
{
尝试
{
if(Convert.ToDecimal(Label1.Text)>0)
{
string proid=Application[“Id”].ToString();
con.Open();
da=新的SqlDataAdapter(“从rsa\u addtocart tpd中选择tpd.productid、tpd.productname、tpd.ProductImage、tpd.price、tpd.qty、tpd.totalcost、tpd.cdate,其中tpd.productid=“+proid+”,con);
ds=新数据集();
da.填充(ds,“待定”);
if(ds.Tables.Count>0&&ds.Tables[“tbl_tp”].Rows.Count>0)
{
使用(var command=new SqlCommand(“插入rsa_Orderconfirmation(UserId、productid、productname、ProductImage、price、qty、totalcost、cdate)值(@UserId、@productid、@productname、@ProductImage、@price、@qty、@totalcost、@cdate)”,con)
{
for(int z=0;z
在rsa_Orderconfirmation中插入值时出现错误,如位置1处没有行。请引导我解决这个问题