Nugs
asked on
Add dynamic column to DataRow
I need some help with a mehtod i am writing to build a dataset of cart items from a catalog.
This is a protion of the method:
-------------------------- ---------- ---------- ---------- ---------- -----
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC atalog"]);
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------ ---------- ---------- ----------
DataRow[] CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr);
CartRows.Add(CartRow[0]);
}
return CartRows;
-------------------------- ---------- ---------- ---------- ---------- -----
CartItemsis a string array of product ID's that the user currently has in there shopping cart.
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr); //<-- Finds the specific product and adds it to the CartRow DataRow[]
CartRows.Add(CartRow[0]); //<-- Adds array item 0 to the ArrayList that is returns...
}
First of all this seems kinda awkward way of doing this. TableAdapter.Select() returns a DataRow[] when in reality only one row should ever be found.
So my first question about this is if there is a better way to find and extract the DataRow (single) from the DataTable and then add this row to the ArrayList.
My second question is that once i have the DataRow of the item/product from the DataTable, i want to add a column to it and set the value to this datarow column for each item so that when it is bound to my datalist on my page i have this column available.
I tried doing something like this, but no luck:
-------------------------- ---------- ---------- ---------- ---------- -----
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC atalog"]);
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste m.Decimal" );
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 1;
TableAdapter.Columns.Add(c olumn);
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------ ---------- ---------- ----------
DataRow[] CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr);
CartRow[0]["Fld_CartQty"] = 2; //<-- This is where i wanted to add a value to the column for a specific product that is found
CartRows.Add(CartRow[0]);
}
return CartRows;
-------------------------- ---------- ---------- ---------- ---------- -----
And onDataBound of the datalist i try to access this new column to place the value of it into a textbox:
-------------------------- ---------- ---------- ---------- ---------- -----
TextBox CartQuantity = dli.FindControl("txtCartQt y") as TextBox;
if (CartQuantity != null)
{ CartQuantity.Text = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_CartQty")); }
-------------------------- ---------- ---------- ---------- ---------- -----
I then get this exception each and every time:
-------------------------- ---------- ---------- ---------- ---------- -----
DataBinding: 'ClassLib.DataSet+TBL_Inve ntoryRow' does not contain a property with the name 'Fld_CartQty'."
-------------------------- ---------- ---------- ---------- ---------- -----
So back to the questions:
1) i need to know if there is a better way topull one row from a DataTable rather than using the Select class which returns a DataRow[]
2) I need to dynamically add a column to the DataRow and update the value of it for each items in the loop...
Help with these two things would be great..
Thanks,
Nugs
This is a protion of the method:
--------------------------
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------
DataRow[] CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRows.Add(CartRow[0]);
}
return CartRows;
--------------------------
CartItemsis a string array of product ID's that the user currently has in there shopping cart.
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRows.Add(CartRow[0]); //<-- Adds array item 0 to the ArrayList that is returns...
}
First of all this seems kinda awkward way of doing this. TableAdapter.Select() returns a DataRow[] when in reality only one row should ever be found.
So my first question about this is if there is a better way to find and extract the DataRow (single) from the DataTable and then add this row to the ArrayList.
My second question is that once i have the DataRow of the item/product from the DataTable, i want to add a column to it and set the value to this datarow column for each item so that when it is bound to my datalist on my page i have this column available.
I tried doing something like this, but no luck:
--------------------------
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 1;
TableAdapter.Columns.Add(c
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------
DataRow[] CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRow[0]["Fld_CartQty"] = 2; //<-- This is where i wanted to add a value to the column for a specific product that is found
CartRows.Add(CartRow[0]);
}
return CartRows;
--------------------------
And onDataBound of the datalist i try to access this new column to place the value of it into a textbox:
--------------------------
TextBox CartQuantity = dli.FindControl("txtCartQt
if (CartQuantity != null)
{ CartQuantity.Text = Convert.ToString(DataBinde
--------------------------
I then get this exception each and every time:
--------------------------
DataBinding: 'ClassLib.DataSet+TBL_Inve
--------------------------
So back to the questions:
1) i need to know if there is a better way topull one row from a DataTable rather than using the Select class which returns a DataRow[]
2) I need to dynamically add a column to the DataRow and update the value of it for each items in the loop...
Help with these two things would be great..
Thanks,
Nugs
ASKER
surajguptha if you look a little further down my question you will see that this is exactly what i am doing. i am adding the column to the datatable before i use the select to find the one record i am looking for.
When i attempt to use the value in this field i get an exception: DataBinding: 'ClassLib.DataSet+TBL_Inve ntoryRow' does not contain a property with the name 'Fld_CartQty'."
So problem still not solved here...
Nugs
When i attempt to use the value in this field i get an exception: DataBinding: 'ClassLib.DataSet+TBL_Inve
So problem still not solved here...
Nugs
Just before you bind the datalist to ur DataTable make sure that this column exists
ASKER
Well this is exactly my problem, it doesn't... And i don't know why it doesn't... It should...
Nugs
Nugs
You are adding a new column to the datatable. are you binding the datalist after adding the new column or before??
If it is after, i think after you have added your column it doesnt get saved back to the session or even if it is saved by reference, the session variable may be overrwritten again. You have to check all the places where you are storing this to the session to make sure that the column exists
If it is after, i think after you have added your column it doesnt get saved back to the session or even if it is saved by reference, the session variable may be overrwritten again. You have to check all the places where you are storing this to the session to make sure that the column exists
ASKER
This should return a row of data + the new Fld_CartQty column.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
...
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC atalog"]);
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste m.Decimal" );
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 25;
// Add the column to the table.
TableAdapter.Columns.Add(c olumn);
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------ ---------- ---------- ----------
DataRow CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr)[0 ];
CartRow["Fld_CartQty"] = 3;
CartRows.Add(CartRow);
}
return CartRows;
...
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
I then bind the rows returned to the datalist:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
...
DLCart.DataSource = BuildCatalog.GetCartItems( );
DLCart.DataBind();
...
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
And then on item data bound of the DataList i attempt to access these coumns:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
...
string ProdID = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_ProdID"));
string CatID = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_Cat"));
string SubCatID = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_SubCat"));
string Temp = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_CartQty")); //<-- the fails
...
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Nugs
--------------------------
...
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 25;
// Add the column to the table.
TableAdapter.Columns.Add(c
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------
DataRow CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRow["Fld_CartQty"] = 3;
CartRows.Add(CartRow);
}
return CartRows;
...
--------------------------
I then bind the rows returned to the datalist:
--------------------------
...
DLCart.DataSource = BuildCatalog.GetCartItems(
DLCart.DataBind();
...
--------------------------
And then on item data bound of the DataList i attempt to access these coumns:
--------------------------
...
string ProdID = Convert.ToString(DataBinde
string CatID = Convert.ToString(DataBinde
string SubCatID = Convert.ToString(DataBinde
string Temp = Convert.ToString(DataBinde
...
--------------------------
Nugs
Nugs,
the problem is when we look at these three snippets as three independant pieces of code, it would look like everything is correct. What is most important is under what events these pieces of code are called. What events trigger these codes and in which order
the problem is when we look at these three snippets as three independant pieces of code, it would look like everything is correct. What is most important is under what events these pieces of code are called. What events trigger these codes and in which order
ASKER
I am not storing any of this in session... the mehtod BuildCatalog.GetCartItems( ); returns a ArrayList of datarows as i have shown above. This array list should contain the datarows with the new column.
Nugs
Nugs
Check in the return CartRows; statement if this column is present ??
ASKER
ok...
MyPage.aspx.cs
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
protected void Page_Load(object sender, EventArgs e)
{
DLCart.DataSource = BuildCatalog.GetCartItems( );
DLCart.DataBind();
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Class.cs
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
public ArrayList GetCartItems()
{
...
...
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC atalog"]);
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste m.Decimal" );
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 25;
// Add the column to the table.
TableAdapter.Columns.Add(c olumn);
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------ ---------- ---------- ----------
DataRow CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr)[0 ];
CartRow["Fld_CartQty"] = 3;
CartRows.Add(CartRow);
}
return CartRows;
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
MyPage.aspx.cs OnDataBound event of the datalist.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
protected void ItemDataBound(object sender, DataListItemEventArgs e)
{
string ProdID = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_ProdID"));
string CatID = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_Cat"));
string SubCatID = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_SubCat"));
string Temp = Convert.ToString(DataBinde r.Eval(e.I tem.DataIt em, "Fld_CartQty")); //<--New column
DataListItem dli = e.Item;
switch (dli.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingIt em:
{
//FILL QUANTITY TEXTBOX FOR CART ITEM
TextBox CartQuantity = dli.FindControl("txtCartQt y") as TextBox;
if (CartQuantity != null)
{
CartQuantity.Text = Temp;
}
break;
}
}
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
Nugs
MyPage.aspx.cs
--------------------------
protected void Page_Load(object sender, EventArgs e)
{
DLCart.DataSource = BuildCatalog.GetCartItems(
DLCart.DataBind();
}
--------------------------
Class.cs
--------------------------
public ArrayList GetCartItems()
{
...
...
DataTable TableAdapter = new DataTable();
TableAdapter = (DataTable)(Session["FullC
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 25;
// Add the column to the table.
TableAdapter.Columns.Add(c
//GET EACH PRODUCT IN LIST AND BUILD ARRAYLIST FROM THEM
//------------------------
DataRow CartRow;
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRow["Fld_CartQty"] = 3;
CartRows.Add(CartRow);
}
return CartRows;
}
--------------------------
MyPage.aspx.cs OnDataBound event of the datalist.
--------------------------
protected void ItemDataBound(object sender, DataListItemEventArgs e)
{
string ProdID = Convert.ToString(DataBinde
string CatID = Convert.ToString(DataBinde
string SubCatID = Convert.ToString(DataBinde
string Temp = Convert.ToString(DataBinde
DataListItem dli = e.Item;
switch (dli.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingIt
{
//FILL QUANTITY TEXTBOX FOR CART ITEM
TextBox CartQuantity = dli.FindControl("txtCartQt
if (CartQuantity != null)
{
CartQuantity.Text = Temp;
}
break;
}
}
}
--------------------------
Nugs
ASKER
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr)[0 ]; //<--- Fld_CartQty Exists in this row
CartRow["Fld_CartQty"] = 3;
CartRows.Add(CartRow);
}
return CartRows; //<-- Fld_CartQty does not seem to exist in the returns ArrayList
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRow["Fld_CartQty"] = 3;
CartRows.Add(CartRow);
}
return CartRows; //<-- Fld_CartQty does not seem to exist in the returns ArrayList
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No, CartRow does not contain the new column even through TableAdapter.Select(strFil terExpr)[0 ] does... and it is what is being added to the DataRow (CartRow)
Nugs
Nugs
Hey if CartRow doesnt have the Fld_CartQty column how come it allows you to set the value CartRow["Fld_CartQty"] = 3; ???
ASKER
Sorry, correction... CartRow always contains the column but it seems to disapear when being added to the ArrayList.. CartRows.Add(CartRow);
Nugs
Nugs
ArrayList CartRows = new ArrayList();
Try
Inread of CartRows as an arrayList can you try making it into List<DataRows> or DataRow[] ??
Try
Inread of CartRows as an arrayList can you try making it into List<DataRows> or DataRow[] ??
ASKER
I tried adding the row to a new DataTable that i wanted tot return:
DataRow CartRow;
DataTable NewTable = new DataTable();
DataRow[] CartRows; //= new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr)[0 ];
CartRow["Fld_CartQty"] = ci;
NewTable.Rows.Add(CartRow) ; //<-- get exception here (This row already belongs to another table.)
}
return NewTable;
DataRow CartRow;
DataTable NewTable = new DataTable();
DataRow[] CartRows; //= new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRow["Fld_CartQty"] = ci;
NewTable.Rows.Add(CartRow)
}
return NewTable;
before this line NewTable.Rows.Add(CartRow) ; add another line
TableAdapter.Rows.Remove(C artRow);
Why dont you just use a DataRow[] or List<DataRows> instead?
TableAdapter.Rows.Remove(C
Why dont you just use a DataRow[] or List<DataRows> instead?
ASKER
could you show me some example code of how to place the DataRow in a DatRow[] as you suggested?
Nugs
Nugs
Did you try this?
before this line NewTable.Rows.Add(CartRow) ; add another line
TableAdapter.Rows.Remove(C artRow);
before this line NewTable.Rows.Add(CartRow)
TableAdapter.Rows.Remove(C
ASKER
DataRow CartRow;
DataTable NewTable = new DataTable();
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr)[0 ];
CartRow["Fld_CartQty"] = ci;
TableAdapter.Rows.Remove(C artRow);
NewTable.Rows.Add(CartRow) ; //<-- Still same error here
}
DataTable NewTable = new DataTable();
ArrayList CartRows = new ArrayList();
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRow["Fld_CartQty"] = ci;
TableAdapter.Rows.Remove(C
NewTable.Rows.Add(CartRow)
}
ASKER
i got something going here:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
if (TableAdapter.Columns["Fld _CartQty"] == null)
{
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste m.Int32");
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 25;
// Add the column to the table.
TableAdapter.Columns.Add(c olumn);
}
DataRow CartRow;
DataTable NewTable = new DataTable();
NewTable = TableAdapter.Clone(); //<-- clone the scheme over
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil terExpr)[0 ];
CartRow["Fld_CartQty"] = ci;
NewTable.ImportRow(CartRow ); //<-- import the row...
}
return NewTable;
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
--------------------------
if (TableAdapter.Columns["Fld
{
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("Syste
column.AllowDBNull = false;
column.Caption = "Fld_CartQty";
column.ColumnName = "Fld_CartQty";
column.DefaultValue = 25;
// Add the column to the table.
TableAdapter.Columns.Add(c
}
DataRow CartRow;
DataTable NewTable = new DataTable();
NewTable = TableAdapter.Clone(); //<-- clone the scheme over
string strFilterExpr = "";
foreach (string ci in CartItems)
{
strFilterExpr = "Fld_ProdID = " + ci;
CartRow = TableAdapter.Select(strFil
CartRow["Fld_CartQty"] = ci;
NewTable.ImportRow(CartRow
}
return NewTable;
--------------------------
ASKER
thanks for all your help...
Nugs
Nugs
>> Well a select is supposed to accomodate the fact that a select COULD return more than a record. Thats why it is designed to return an array of DataRows.
>>Since Select() returns an array and you are sure that you WOULD only get a record back. just say DataAdaptar.Select()[0] to access just the first record
2)I need to dynamically add a column to the DataRow and update the value of it for each items in the loop...
>> You cannot add a column to the datarow but u can add a coulum to the datatable using. DataTable.Columns.Add("Col