.

Saturday, July 12, 2008

Create a DataTable in .Net

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

No comments:

.