Google Brother Up

Showing posts with label DataTable. Show all posts
Showing posts with label DataTable. Show all posts

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();
}

Create a DataTable


public void createSecondDataTable()
{
// Create a new DataTable.
System.Data.DataTable table = new DataTable("UpdatedManageShoppingCartTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;


//Add 1st Column
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ProductID";
//column.ReadOnly = true;
column.Unique = true;
//Add Column to table
table.Columns.Add(column);


//Add 2nd Column
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ProductName";
//column.ReadOnly = true;
column.Unique = false;
//Add Column to table
table.Columns.Add(column);


//Add 3rd Column
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "Quantity";
//column.ReadOnly = true;
column.Unique = false;
//Add Column to table
table.Columns.Add(column);


//Add 4th Column
column = new DataColumn();
column.DataType = System.Type.GetType("System.Double");
column.ColumnName = "Price";
//column.ReadOnly = true;
column.Unique = false;
//Add Column to table
table.Columns.Add(column);


//Add 5th Column
column = new DataColumn();
column.DataType = System.Type.GetType("System.Double");
column.ColumnName = "NetPrice";
//column.ReadOnly = true;
column.Unique = false;


//Add Column to table
table.Columns.Add(column);

//Make the ProductID column the primary column
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["ProductID"];
table.PrimaryKey = PrimaryKeyColumns;


//Instantiate the dataset
//DataSet dataset = new DataSet();
//Add datatable to the dataset
//dataset.Tables.Add(table);
Session["UpdatedManageShoppingCartTableHolder"] = table;
}

Drop Down List

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