Google Brother Up

2008/05/22

Add Data to the DataTable (in earlier post)

public void addDataToDataTable()
{
FreshCart fc = new FreshCart();
DataSet ds = new DataSet();
ViewState["ProdID"] = Request.QueryString["ProductID"];
ds = fc.GetProductDetailsByProductID(Convert.ToInt32(ViewState["ProdID"]));
System.Data.DataTable table = new DataTable("ManageShoppingCartTable");
table = (DataTable)(Session["DataTableHolder"]);
DataRow row;
row = table.NewRow();
row["ProductID"] = Convert.ToInt32(ds.Tables[0].Rows[0][("ProductID")]);
row["ProductName"] = ds.Tables[0].Rows[0][("ProductName")].ToString().Trim();
//if (table.Rows.Count < 1)
//{
row["Quantity"] = 1;
//}
//else if (table.Rows.Count >= 1)
//{
// row["Quantity"] = Convert.ToInt32(gvManageShoppingCart.FindControl("txtQuantity.Text"));
//}
row["Price"] = Convert.ToDouble(ds.Tables[0].Rows[0][("Price")].ToString().Trim());
row["NetPrice"] = Convert.ToInt32(row["Quantity"].ToString()) * Convert.ToDouble(row["Price"]);
//row["NetPrice"] = 2*Convert.ToDouble(row["Price"]);
//int s = 0;
int rowVal = Convert.ToInt32(row["ProductID"]);
bool blnIsProductExist = false;
// if current product id is already present then dont insert the row.
if (Convert.ToInt32(table.Rows.Count) == 0)
{
table.Rows.Add(row);
}
else
{
for (int i = 0; i < table.Rows.Count; i++)
{
//int rowVal = Convert.ToInt32(row["ProductID"]);
if (Convert.ToInt32(table.Rows[i][0]) == rowVal)
{
//s = s + 1;
blnIsProductExist = true;
}
else
{
//s = 0;
}
}


if (!blnIsProductExist)
{
lblMessageUpdate.Visible = false;
table.Rows.Add(row);
}

else
{
lblMessageUpdate.Visible = true;
lblMessageUpdate.Text = "The data is already present and cannot be updated".ToString();
}
}
lblFlag.Text = "1"; //set the flag to avoid postback after populating the gridview
Session["DataTableHolder"] = table;
gvManageShoppingCart.DataSource = ((DataTable)table).DefaultView;
gvManageShoppingCart.DataBind();
}

No comments:

Drop Down List

1. http://www.janetsystems.co.uk/Articles/NetArticles/tabid/74/itemid/161/modid/449/Default.aspx