Author Topic: Datatables and Datarows.  (Read 4255 times)

0 Members and 1 Guest are viewing this topic.

tjr

  • Guest
Datatables and Datarows.
« on: September 07, 2008, 10:20:47 AM »
I'm trying to take multiple datatables and combine them into one "matrix" datatable. Using the code below it fails on the following line:
Code: [Select]
ndr[tdt.TableName] = dr["qty"].ToString(); Where I had previously set the column name to the table name and now I'm trying to access it.

Any thoughts?
Code: [Select]
       public DataTable getBomTable2()
        {
            int maxrows = 0;
            DataTable returnDT = new DataTable();
            List<string> tableNames = new List<string>();

            //Loop through each datatable and find the greatest
            //possible row count.
            foreach (DataTable tdt in this.tempDS.Tables)
            {
                if (tdt.Rows.Count > maxrows)
                {
                    maxrows = tdt.Rows.Count;
                }
            }
            //Loop Through each datatable and collect tablenames.
            foreach (DataTable tdt in this.tempDS.Tables)
            {
                tableNames.Add(tdt.TableName);
            }

            #region DataColumnAdd
           
             //Add the BOM columns in reverse order.
            for (int i=0; i < tableNames.Count-1; i++)
            {
                DataColumn dc = new DataColumn();
                dc.DataType = typeof(string);
                dc.ColumnName = tableNames[(tableNames.Count-1) - i].ToString();
                returnDT.Columns.Add(dc);
            }

            //Add all the other DataColumns
            DataColumn catalogCol = new DataColumn();
            catalogCol.DataType = typeof(string);
            catalogCol.ColumnName = "catalogno";
            returnDT.Columns.Add(catalogCol);

            DataColumn partNoCol = new DataColumn();
            partNoCol.DataType = typeof(string);
            partNoCol.ColumnName = "partno";
            returnDT.Columns.Add(partNoCol);

            DataColumn descCol = new DataColumn();
            descCol.DataType = typeof(string);
            descCol.ColumnName = "description";
            returnDT.Columns.Add(descCol);

            DataColumn lenCol = new DataColumn();
            lenCol.DataType = typeof(string);
            lenCol.ColumnName = "blength";
            returnDT.Columns.Add(lenCol);

            DataColumn widCol = new DataColumn();
            widCol.DataType = typeof(string);
            widCol.ColumnName = "bwidth";
            returnDT.Columns.Add(widCol);

            DataColumn remCol = new DataColumn();
            remCol.DataType = typeof(string);
            remCol.ColumnName = "remark";
            returnDT.Columns.Add(remCol);

            DataColumn refCol = new DataColumn();
            refCol.DataType = typeof(string);
            refCol.ColumnName = "ksp";
            returnDT.Columns.Add(refCol);
            #endregion

            //Combine all datatables.
            foreach (DataTable tdt in this.tempDS.Tables)
            {
                bool found = false;
                foreach (DataRow dr in tdt.Rows)
                {
                    found = false;
                    foreach (DataRow rdr in returnDT.Rows)
                    {
                        if (dr["catalogno"] == rdr["catalogno"] & dr["blength"] == rdr["blength"] & dr["bwidth"] == rdr["bwidth"])
                        {
                            found = true;
                            rdr[tdt.TableName] = dr["qty"].ToString();
                        }
                    }
                    if (found == false)
                    {
                        DataRow ndr = returnDT.Rows.Add();
                        ndr[tdt.TableName] = dr["qty"].ToString();
                        ndr["catalogno"] = dr["catalogno"].ToString();
                        ndr["partno"] = dr["partno"].ToString();
                        ndr["description"] = dr["description"].ToString();
                        ndr["blength"] = dr["blength"].ToString();
                        ndr["bwidth"] = dr["bwidth"].ToString();
                        ndr["remark"] = dr["remark"].ToString();
                        ndr["ksp"] = dr["ksp"].ToString();
                        returnDT.Rows.Add(ndr);
                    }
                }
            }

            return returnDT;
        }

tjr

  • Guest
Re: Datatables and Datarows.
« Reply #1 on: September 07, 2008, 04:21:24 PM »
Went out and cut the grass, weedwhack etc. and it came to me. Sometimes it's good to take a little break.:

Code: [Select]
public DataTable getBomTable2()
        {
            int maxrows = 0;
            DataTable returnDT = new DataTable();
            List<string> tableNames = new List<string>();

            //Loop through each datatable and find the greatest
            //possible row count.
            foreach (DataTable tdt in this.tempDS.Tables)
            {
                if (tdt.Rows.Count > maxrows)
                {
                    maxrows = tdt.Rows.Count;
                }
            }
            //Loop Through each datatable and collect tablenames.
            foreach (DataTable tdt in this.tempDS.Tables)
            {
                tableNames.Add(tdt.TableName);
            }

            #region DataColumnAdd
           
             //Add the BOM columns in reverse order.
            for (int i=0; i < tableNames.Count; i++)
            {
                DataColumn dc = new DataColumn();
                dc.DataType = typeof(string);
                dc.ColumnName = tableNames[(tableNames.Count-1) - i].ToString();
                returnDT.Columns.Add(dc);
            }


            //Add all the other DataColumns
            DataColumn catalogCol = new DataColumn();
            catalogCol.DataType = typeof(string);
            catalogCol.ColumnName = "catalogno";
            returnDT.Columns.Add(catalogCol);

            DataColumn partNoCol = new DataColumn();
            partNoCol.DataType = typeof(string);
            partNoCol.ColumnName = "partno";
            returnDT.Columns.Add(partNoCol);

            DataColumn descCol = new DataColumn();
            descCol.DataType = typeof(string);
            descCol.ColumnName = "description";
            returnDT.Columns.Add(descCol);

            DataColumn lenCol = new DataColumn();
            lenCol.DataType = typeof(string);
            lenCol.ColumnName = "blength";
            returnDT.Columns.Add(lenCol);

            DataColumn widCol = new DataColumn();
            widCol.DataType = typeof(string);
            widCol.ColumnName = "bwidth";
            returnDT.Columns.Add(widCol);

            DataColumn remCol = new DataColumn();
            remCol.DataType = typeof(string);
            remCol.ColumnName = "remark";
            returnDT.Columns.Add(remCol);

            DataColumn refCol = new DataColumn();
            refCol.DataType = typeof(string);
            refCol.ColumnName = "ksp";
            returnDT.Columns.Add(refCol);
            #endregion

            returnDT.AcceptChanges();

            //Combine all datatables.
            foreach (DataTable tdt in this.tempDS.Tables)
            {
                bool found = false;
                foreach (DataRow dr in tdt.Rows)
                {
                    found = false;
                    foreach (DataRow rdr in returnDT.Rows)
                    {
                        if (dr["catalogno"] == rdr["catalogno"] & dr["blength"] == rdr["blength"] & dr["bwidth"] == rdr["bwidth"])
                        {
                            found = true;
                            rdr[tdt.TableName] = dr["qty"].ToString();
                        }
                    }
                    if (found == false)
                    {
                        DataRow ndr = returnDT.NewRow();
                        ndr[tdt.TableName.ToString()] = dr["qty"].ToString();
                        ndr["catalogno"] = dr["catalogno"].ToString();
                        ndr["partno"] = dr["partno"].ToString();
                        ndr["description"] = dr["description"].ToString();
                        ndr["blength"] = dr["blength"].ToString();
                        ndr["bwidth"] = dr["bwidth"].ToString();
                        ndr["remark"] = dr["remark"].ToString();
                        ndr["ksp"] = dr["ksp"].ToString();
                        returnDT.Rows.Add(ndr);
                        found = true;
                    }
                }
            }
            DataView obDataView = new DataView(returnDT);
            obDataView.Sort = "partno ASC";
            return this.CreateTable(obDataView);
        }

Draftek

  • Guest
Re: Datatables and Datarows.
« Reply #2 on: September 09, 2008, 08:11:38 AM »
I'd recommend using sql statements instead of iterating thru database tables. It's much cleaner to maintain and the performance will improve.

Also - I doubt this line is producing the result your expecting:
Quote
if (dr["catalogno"] == rdr["catalogno"] & dr["blength"] == rdr["blength"] & dr["bwidth"] == rdr["bwidth"])

Glenn R

  • Guest
Re: Datatables and Datarows.
« Reply #3 on: September 09, 2008, 09:11:55 AM »
I'd recommend using sql statements instead of iterating thru database tables. It's much cleaner to maintain and the performance will improve.

Also - I doubt this line is producing the result your expecting:
Quote
if (dr["catalogno"] == rdr["catalogno"] & dr["blength"] == rdr["blength"] & dr["bwidth"] == rdr["bwidth"])

I agree on both counts and good catch on the second...nasty gotcha in C based languages.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Datatables and Datarows.
« Reply #4 on: September 09, 2008, 09:27:22 AM »
Use LinQ
« Last Edit: September 09, 2008, 09:30:27 AM by Daniel »

Glenn R

  • Guest
Re: Datatables and Datarows.
« Reply #5 on: September 09, 2008, 09:28:56 AM »
Even better idea - what Dan said.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Datatables and Datarows.
« Reply #6 on: September 09, 2008, 09:36:38 AM »
I’d help with an example but I’m packing for my move to Detroit  :-o

Draftek

  • Guest
Re: Datatables and Datarows.
« Reply #7 on: September 09, 2008, 09:46:28 AM »
Yeah, thanks. Been meaning to check that out...

Glenn R

  • Guest
Re: Datatables and Datarows.
« Reply #8 on: September 09, 2008, 09:57:24 AM »
Leaving honkers Dan?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: Datatables and Datarows.
« Reply #9 on: September 09, 2008, 10:18:15 AM »
Leaving honkers Dan?

Yep, The wife got a job as a regional tax manager, they are sending her (and family) to Detroit for training for about 6 months. Then it’s back to this side of the world (Shanghai).

Glenn R

  • Guest
Re: Datatables and Datarows.
« Reply #10 on: September 09, 2008, 10:20:52 AM »
Travel broadens the mind as they say. Have fun, but the moving sure blows.

tjr

  • Guest
Re: Datatables and Datarows.
« Reply #11 on: September 09, 2008, 11:26:47 AM »
I'd recommend using sql statements instead of iterating thru database tables. It's much cleaner to maintain and the performance will improve.
Hadn't even thought about that. Thanks for the tip.

Also - I doubt this line is producing the result your expecting:
Quote
if (dr["catalogno"] == rdr["catalogno"] & dr["blength"] == rdr["blength"] & dr["bwidth"] == rdr["bwidth"])
Yeah I caught that and fixed it the other day.