Unable to update the datagrid!!

Jan 14 2009 3:05 AM

Hi to all,

I have a problem in updating the datagrid.

The scenerio is: I am having a datagrid on my page.I am basically working on shopping cart.Whenever user selects any the product it should be displayed in the datagrid(ie my cart).Suppose the user continues the shopping and he again selects one of the product.Then it should be added below the first row in the datagrid.

But I am not getting this result.Whenever I add another item in my cart ,it will replace the previous one.

I have tried it as:

protected void Page_Load(object sender, EventArgs e)

{

if (Session["Cart"] == null)

{

get_data();

Session["Cart"] = dt;

}

else

{

dt = (DataTable)Session["Cart"];

get_data();

// Session["Cart"] = Session["Cart"] + dt;

}

}

public void get_data()

{

dt = new DataTable();

dt.Columns.Add("REF", typeof(string));

dt.Columns.Add("Description", typeof(string));

dt.Columns.Add("QTY", typeof(int));

dt.Columns.Add("Price", typeof(float));

dt.Columns.Add("Cost", typeof(float));

pro_id = Request.QueryString["pr_id"];

quanti = Convert.ToInt32(Request.QueryString["quant"]);

SqlConnection con = new SqlConnection("Server=.; Database=eclsc; Trusted_Connection=yes");

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = "select ICEMACHINES.product_id,ICEMACHINES.sh_desc,ICEMACHINES.price from ICEMACHINES where ICEMACHINES.product_id LIKE @product_id union select GLASSWARE.product_id,GLASSWARE.sh_desc,GLASSWARE.price from GLASSWARE where GLASSWARE.product_id LIKE @product_id ";

cmd.Parameters.Add("@product_id", SqlDbType.NVarChar, 50).Value = pro_id;

cmd.Connection.Open();

SqlDataReader rdr = cmd.ExecuteReader();

ArrayList arRole1 = new ArrayList();

while (rdr.Read())

{

desc = (rdr["sh_desc"]).ToString();

id = (rdr["product_id"]).ToString();

unitprice = Convert.ToSingle((rdr["price"]));

cost = unitprice * quanti;

arRole1.Add(rdr["product_id"]);

arRole1.Add(rdr["sh_desc"]);

}

cmd.Connection.Close();

DataRow myrow = dt.NewRow();

myrow["REF"] = id;

myrow["Description"] = desc;

myrow["QTY"] = quanti;

myrow["Price"] = unitprice;

myrow["Cost"] = cost;

dt.Rows.Add(myrow);

dt.AcceptChanges();

DataGrid2.DataSource = dt;

DataGrid2.DataBind();

}

Please give me some idea where I am lacking.

Thanks in advance!!


Answers (2)