?
Solved

Add dynamic column to DataRow

Posted on 2007-10-14
23
Medium Priority
?
12,896 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Nugs
  • 13
  • 10
23 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20075765
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
 
LVL 2

Author Comment

by:Nugs
ID: 20079483
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080154
Just before you bind the datalist to ur DataTable make sure that this column exists
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:Nugs
ID: 20080303
Well this is exactly my problem, it doesn't... And i don't know why it doesn't... It should...

Nugs
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080357
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
 
LVL 2

Author Comment

by:Nugs
ID: 20080364
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080380
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
 
LVL 2

Author Comment

by:Nugs
ID: 20080384
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080395
Check in the  return CartRows; statement if this column is present ??
0
 
LVL 2

Author Comment

by:Nugs
ID: 20080407
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
 
LVL 2

Author Comment

by:Nugs
ID: 20080433
           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
 
LVL 21

Accepted Solution

by:
surajguptha earned 2000 total points
ID: 20080458
<<-- 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
 
LVL 2

Author Comment

by:Nugs
ID: 20080495
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080508
Hey if CartRow doesnt have the Fld_CartQty column how come it allows you to set the value CartRow["Fld_CartQty"] = 3; ???
0
 
LVL 2

Author Comment

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

Nugs
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080538
ArrayList CartRows = new ArrayList();

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

Author Comment

by:Nugs
ID: 20080658
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
 
LVL 21

Expert Comment

by:surajguptha
ID: 20080688
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
 
LVL 2

Author Comment

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

Nugs
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20081617
Did you try this?
before this line NewTable.Rows.Add(CartRow); add another line
TableAdapter.Rows.Remove(CartRow);
0
 
LVL 2

Author Comment

by:Nugs
ID: 20081646
           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
 
LVL 2

Author Comment

by:Nugs
ID: 20081850
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
 
LVL 2

Author Comment

by:Nugs
ID: 20082032
thanks for all your help...

Nugs
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question