获取gridview中每行的Datakeynames值

时间:2015-06-23 18:53:34

标签: c# asp.net

我希望从一个表中获取所有数据并将其存储在数据集中,并且必须将这些值插入另一个表中。我使用gridview的datakeynames选择了值,CartGridview中有更多记录,但它只需要一条最新值的记录如何使用datakeynames从gridview获取所有记录?它只插入最新值。

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)
    {
    }
}

在rsa_Orderconfirmation中插入值时出现错误,如位置1处没有行。请指导我解决此问题。

0 个答案:

没有答案