Link to home
Start Free TrialLog in
Avatar of Nugs
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["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
Avatar of surajguptha
surajguptha
Flag of United States of America image

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");




Avatar of Nugs
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_InventoryRow' does not contain a property with the name 'Fld_CartQty'."

So problem still not solved here...

Nugs
Just before you bind the datalist to ur DataTable make sure that this column exists
Avatar of Nugs

ASKER

Well this is exactly my problem, it doesn't... And i don't know why it doesn't... It should...

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
Avatar of Nugs

ASKER

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
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
Avatar of Nugs

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
Check in the  return CartRows; statement if this column is present ??
Avatar of Nugs

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["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
Avatar of Nugs

ASKER

           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
ASKER CERTIFIED SOLUTION
Avatar of surajguptha
surajguptha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nugs

ASKER

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
Hey if CartRow doesnt have the Fld_CartQty column how come it allows you to set the value CartRow["Fld_CartQty"] = 3; ???
Avatar of Nugs

ASKER

Sorry, correction... CartRow always contains the column but it seems to disapear when being added to the ArrayList.. CartRows.Add(CartRow);

Nugs
ArrayList CartRows = new ArrayList();

Try
Inread of CartRows as an arrayList can you try making it into List<DataRows> or DataRow[] ??
Avatar of Nugs

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(strFilterExpr)[0];
                CartRow["Fld_CartQty"] = ci;

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

            return NewTable;
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?
Avatar of Nugs

ASKER

could you show me some example code of how to place the DataRow in a DatRow[] as you suggested?

Nugs
Did you try this?
before this line NewTable.Rows.Add(CartRow); add another line
TableAdapter.Rows.Remove(CartRow);
Avatar of Nugs

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(strFilterExpr)[0];
                CartRow["Fld_CartQty"] = ci;

                TableAdapter.Rows.Remove(CartRow);
                NewTable.Rows.Add(CartRow); //<-- Still same error here
            }
Avatar of Nugs

ASKER

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;
---------------------------------------------------------------------------------------------------------------------
Avatar of Nugs

ASKER

thanks for all your help...

Nugs