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["FullCatalog"]);

            //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(strFilterExpr);
                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(strFilterExpr); //<-- 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["FullCatalog"]);

            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.Decimal");
            column.AllowDBNull = false;
            column.Caption = "Fld_CartQty";
            column.ColumnName = "Fld_CartQty";
            column.DefaultValue = 1;

          TableAdapter.Columns.Add(column);


            //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(strFilterExpr);
            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("txtCartQty") as TextBox;
                if (CartQuantity != null)
                { CartQuantity.Text = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Fld_CartQty")); }
-----------------------------------------------------------------------


I then get this exception each and every time:
-----------------------------------------------------------------------
DataBinding: 'ClassLib.DataSet+TBL_InventoryRow' 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
LVL 2
NugsAsked:
Who is Participating?
 
surajgupthaCommented:
<<-- Can you check if it exits in CartRow and after every add into CartRows???

           foreach (string ci in CartItems)
            {
                strFilterExpr = "Fld_ProdID = " + ci;
                CartRow = TableAdapter.Select(strFilterExpr)[0]; //<--- Fld_CartQty Exists in this row
                CartRow["Fld_CartQty"] = 3;
                CartRows.Add(CartRow); <<-- Can you check if it exits in CartRow and after every add into CartRows???
            }

0
 
surajgupthaCommented:
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[]
>> 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("Column Name");




0
 
NugsAuthor Commented:
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_InventoryRow' does not contain a property with the name 'Fld_CartQty'."

So problem still not solved here...

Nugs
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
surajgupthaCommented:
Just before you bind the datalist to ur DataTable make sure that this column exists
0
 
NugsAuthor Commented:
Well this is exactly my problem, it doesn't... And i don't know why it doesn't... It should...

Nugs
0
 
surajgupthaCommented:
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
0
 
NugsAuthor Commented:
This should return a row of data + the new Fld_CartQty column.

--------------------------------------------------------------------------------------------------------------------
...
            DataTable TableAdapter = new DataTable();
            TableAdapter = (DataTable)(Session["FullCatalog"]);

            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.Decimal");
            column.AllowDBNull = false;
            column.Caption = "Fld_CartQty";
            column.ColumnName = "Fld_CartQty";
            column.DefaultValue = 25;

            // Add the column to the table.
            TableAdapter.Columns.Add(column);

            //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(strFilterExpr)[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(DataBinder.Eval(e.Item.DataItem, "Fld_ProdID"));
        string CatID = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Fld_Cat"));
        string SubCatID = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Fld_SubCat"));
        string Temp = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Fld_CartQty")); //<-- the fails
...
--------------------------------------------------------------------------------------------------------------------

Nugs
0
 
surajgupthaCommented:
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
0
 
NugsAuthor Commented:
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
0
 
surajgupthaCommented:
Check in the  return CartRows; statement if this column is present ??
0
 
NugsAuthor Commented:
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["FullCatalog"]);

            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.Decimal");
            column.AllowDBNull = false;
            column.Caption = "Fld_CartQty";
            column.ColumnName = "Fld_CartQty";
            column.DefaultValue = 25;

            // Add the column to the table.
            TableAdapter.Columns.Add(column);

            //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(strFilterExpr)[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(DataBinder.Eval(e.Item.DataItem, "Fld_ProdID"));
        string CatID = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Fld_Cat"));
        string SubCatID = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Fld_SubCat"));
        string Temp = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Fld_CartQty")); //<--New column

        DataListItem dli = e.Item;
        switch (dli.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
            {
               //FILL QUANTITY TEXTBOX FOR CART ITEM
                TextBox CartQuantity = dli.FindControl("txtCartQty") as TextBox;
                if (CartQuantity != null)
                {
                    CartQuantity.Text = Temp;
                }
               

                break;
            }
        }
    }
-------------------------------------------------------------------------------------------------------------------

Nugs
0
 
NugsAuthor Commented:
           foreach (string ci in CartItems)
            {
                strFilterExpr = "Fld_ProdID = " + ci;
                CartRow = TableAdapter.Select(strFilterExpr)[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
0
 
NugsAuthor Commented:
No, CartRow does not contain the new column even through TableAdapter.Select(strFilterExpr)[0] does... and it is what is being added to the DataRow (CartRow)

Nugs
0
 
surajgupthaCommented:
Hey if CartRow doesnt have the Fld_CartQty column how come it allows you to set the value CartRow["Fld_CartQty"] = 3; ???
0
 
NugsAuthor Commented:
Sorry, correction... CartRow always contains the column but it seems to disapear when being added to the ArrayList.. CartRows.Add(CartRow);

Nugs
0
 
surajgupthaCommented:
ArrayList CartRows = new ArrayList();

Try
Inread of CartRows as an arrayList can you try making it into List<DataRows> or DataRow[] ??
0
 
NugsAuthor Commented:
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(strFilterExpr)[0];
                CartRow["Fld_CartQty"] = ci;

                NewTable.Rows.Add(CartRow); //<-- get exception here (This row already belongs to another table.)
            }

            return NewTable;
0
 
surajgupthaCommented:
before this line NewTable.Rows.Add(CartRow); add another line
TableAdapter.Rows.Remove(CartRow);

Why dont you just use a DataRow[] or List<DataRows> instead?
0
 
NugsAuthor Commented:
could you show me some example code of how to place the DataRow in a DatRow[] as you suggested?

Nugs
0
 
surajgupthaCommented:
Did you try this?
before this line NewTable.Rows.Add(CartRow); add another line
TableAdapter.Rows.Remove(CartRow);
0
 
NugsAuthor Commented:
           DataRow CartRow;
            DataTable NewTable = new DataTable();
            ArrayList CartRows = new ArrayList();

            string strFilterExpr = "";
            foreach (string ci in CartItems)
            {
                strFilterExpr = "Fld_ProdID = " + ci;
                CartRow = TableAdapter.Select(strFilterExpr)[0];
                CartRow["Fld_CartQty"] = ci;

                TableAdapter.Rows.Remove(CartRow);
                NewTable.Rows.Add(CartRow); //<-- Still same error here
            }
0
 
NugsAuthor Commented:
i got something going here:

---------------------------------------------------------------------------------------------------------------------
            if (TableAdapter.Columns["Fld_CartQty"] == null)
            {
                DataColumn column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.AllowDBNull = false;
                column.Caption = "Fld_CartQty";
                column.ColumnName = "Fld_CartQty";
                column.DefaultValue = 25;

                // Add the column to the table.
                TableAdapter.Columns.Add(column);
            }

            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(strFilterExpr)[0];
                CartRow["Fld_CartQty"] = ci;
               
                NewTable.ImportRow(CartRow); //<-- import the row...
            }

            return NewTable;
---------------------------------------------------------------------------------------------------------------------
0
 
NugsAuthor Commented:
thanks for all your help...

Nugs
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.