Author Topic: Searching DataTable  (Read 2401 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Searching DataTable
« on: March 11, 2010, 03:50:52 PM »
I am stumped on how to get my search to run.  I copied this code from MSDN
Code: [Select]
DataTable table = DataSet1.Tables["Orders"];
            // Presuming the DataTable has a column named Date.
            string expression;
            expression = "Date > #1/1/00#";
            DataRow[] foundRows;

            // Use the Select method to find all rows matching the filter.
            foundRows = table.Select(expression);

            // Print column 0 of each returned row.
            for (int i = 0; i < foundRows.Length; i++)
            {
                Console.WriteLine(foundRows[i][0]);
            }
but when I edit it, I get an error about comparing apples and oranges
here is my code
Code: [Select]
            DataTable table = dataSet1.Tables["DrawingData"];
            DataRow[] foundRow;
            string expression;
            expression = txtDrawingNumber.Text; //"DrawingNumber = " +
            foundRow = table.Select(expression);
            int i = foundRow.Length;
            if (i == 0)
            {
                dataSet1.Tables["DrawingData"].Rows.Add(rw);
                txtDrawingNumber.Text = String.Empty;
                dataSet1.Tables["DrawingData"].WriteXml(@"c:\test.xml");
                txtDrawingNumber.Focus();
            }

At this point I probablly have extra crap in there as I was trying to figure out what was going on.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Glenn R

  • Guest
Re: Searching DataTable
« Reply #1 on: March 11, 2010, 04:17:39 PM »
Hmmm...DataSet, DataTable and a DataView...i did this a few years ago with 1000's of layer names...I'll see if I can dig it up...give me a few...

BTW, you don't mention the error or spefically what you're trying to do Duh...

Glenn R

  • Guest
Re: Searching DataTable
« Reply #2 on: March 11, 2010, 04:27:37 PM »
Duh,

Just had a quick look at my code (it's in a repository and it's late: read - I'm sure I've used a DataView as I found it it was far quicker), but try wrapping you're DrawingNumber in single quotes, ala:

Code: [Select]
"DrawingNumber = '" + SomVariable + "'"

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Searching DataTable
« Reply #3 on: March 12, 2010, 10:04:00 AM »
Thanks Glenn, I will go test that now.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Searching DataTable
« Reply #4 on: March 12, 2010, 10:51:34 AM »
Glenn, you were spot on, that was it.  Now my question is why would that make a difference?  When concatenating strings, did not the .Text return a string?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

fixo

  • Guest
Re: Searching DataTable
« Reply #5 on: March 12, 2010, 12:35:53 PM »
Commandor, how about:

Code: [Select]
expression = "DrawingNumber = '" + DateTime.Parse(txtDrawingNumber.Text.ToShortDateString() + "'";
(not tested)

~'J'~

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Searching DataTable
« Reply #6 on: March 12, 2010, 01:27:42 PM »
Thanks Fixo!  The date time thing was from the example on MSDN, I was just trying to use a string from a textbox
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Glenn R

  • Guest
Re: Searching DataTable
« Reply #7 on: March 12, 2010, 02:43:39 PM »
When you're doing filters like these, you find that you need to single quote the right hand value of the equals expression...I seem to remember this was the case for searching XML with XPATH expressions as well.

Glenn R

  • Guest
Re: Searching DataTable
« Reply #8 on: March 12, 2010, 02:44:26 PM »
To answer your question specifically Duh, yes, .Text did return a string.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Searching DataTable
« Reply #9 on: March 12, 2010, 05:03:11 PM »
Thanks Glenn!  I thought I was doing it right, but I just could not make it work.  the single quote worked the first time through.  My next challenge is to grab the row where it found it and return a column to the right.  More reading tonight
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)