Solved

Help finding row in datatable

Posted on 2007-04-07
6
339 Views
Last Modified: 2010-04-15
I am having trouble finding a row in a datatable

I have a datatable that I build programmatically.

One of the column in the table is a System.Int32 and its called "ID"

The code to set this column in the table is

col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "ID";
        dto.Columns.Add(col);

Following the information found here

http://msdn2.microsoft.com/en-us/library/tat996zc(vs.80).aspx

which indicates I can find a specific row in the datatable by using soemthign like this...

DataRow[] arow = dto.Select("ID = 'prodid'");    //prodid in an int variable

and the supposedly I can update a column in that row with this

arow[0]["Quantity"] = newquant

However whenI try to run this code I get the error..

Exception Details: System.Data.EvaluateException: Cannot perform '=' operation on System.Int32 and System.String.

Im kinda stuck here..I need to be able to find the row in the table where the ID column is equal to the prodid value...
0
Comment
Question by:Prysson
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Yttribium
ID: 18870946
I did this:

                  System.Data.DataView dv = new System.Data.DataView();
                  //new.System.Data.DataColumn dc = new System.Data.DataColumn();
                  dv.Table = new System.Data.DataTable("Data");
                  dv.Table.Columns.Add("col1");
                  dv.Table.Columns.Add("col2");
                  dv.Table.Columns.Add("col3");
                  dv.Table.Rows.Add(new object[] { 1, "a", 3 });
                  dv.Table.Rows.Add(new object[] { 2, "b", 3 });
                  dv.Table.Rows.Add(new object[] { 3, "c" , 3 });

                  //System.Data.DataRow dr = new System.Data.DataRow();
                  System.Data.DataRow[] dr = dv.Table.Select("col1 = 1");
                  MessageBox.Show(dr[0][1].ToString());
                  dr[0][1] = "r";
                  MessageBox.Show(dr[0][1].ToString());

And it worked fine, is this similar to what you want to do?  If not maybe a bit more code from your side might shed more light :)
0
 

Author Comment

by:Prysson
ID: 18870976
Well Im not using a Dataview though I suppose I could...

I build the DataTable with this code...

DataTable dto = new DataTable();
        DataColumn col;

        col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "ID";
        dto.Columns.Add(col);
       
        col = new DataColumn();
        col.DataType = System.Type.GetType("System.String");
        col.ColumnName = "Products";
        dto.Columns.Add(col);

col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "Quantity";
        dto.Columns.Add(col);

Then later on I have a variable

int proid

That int get set and with the value of that int I need to basically find the row in the Datatable that has that value in the "ID" column from the code above.

Once I find that row I then need to change the value of the "Quantity" column to match a seperate variable called
int newquant

At the moment I am stuck trying to get the code right to find the correct row.

DataRow[] arow = dto.Select("ID = 'prodid'");  

is giving me the error I mentioned.
This looks similar to what you are doing but I am guessing my syntax is wrong somehow and I cant figure it out.

DataRow[] arow = dto.Select("ID = prodid");  

doesnt work either but that gives me a different eror...then it says it cant find the column prodid..though my understanding is that ID shoudl be the column and prodid would be the vlue...
I THINK that the problem is that it is readin the 'prodid' as a string value and the "ID" column is actually an Int32.  But prodid is actually a variable. I dont know what the value is in advance so I have to use a variable that gets set...

0
 
LVL 14

Expert Comment

by:steveberzins
ID: 18871016
DataRow[] arow = dto.Select(string.Format("ID = {0:d}", prodid));

in the cases you're using, the first it thinks
(int)ID = (string)'prodid' == won't work since you're comparing an int to a string
in the second, you're asking it to compare the ID column, to the prodid column, again == no worky :)

what you really want is something like (no quotes to make it think 1 is a string)
ID = 1
and not
ID = '1'
or
ID = prodid
0
Technology Partners: 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!

 

Author Comment

by:Prysson
ID: 18871035
Wow that worked great...

If you dont mind could you explain the code a bit what is occuring when you

(string.Format("ID = {0:d}", prodid));

I guess I just being a dunce but Im not sure exactly whats happening there.

Thanks for your help!!!
0
 
LVL 14

Accepted Solution

by:
steveberzins earned 500 total points
ID: 18871175
essentially same as "ID = " + prodid.ToString();

I just like the look of using string.Format...personal preference as much as anything...I can more easily see what the resulting string will be, which is even more important as the number of things you want to put into the string increase. also, if you're doing multiple string concats, one way of optimizing is using string.Format(...), kind of the middle ground between concatenating strings, and using a string builder for really big strings as I see it...if that makes sense...

the number {0} is which parameter number to put in that spot, zero based, and the :d tells it to do a string conversion on an expected integer parameter.

could be done as:
string filter = string.Format("ID = {0}", prodid.ToString());

but I like having the conversion done for me, I think it also handles nulls better, but I'm not 100% sure, except I know that if prodid is null, doing a .ToString() will blow, and I think using the :d, it does it as an empty string, and no bad errors. :)
0
 

Author Comment

by:Prysson
ID: 18871208
Thats great. Thanks again for your help..this one was a bugger.

Thanks also for the additional explanation..allows me to understand what Im doing more clearly in the event I have to use it again.
 Very much appreciated.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
placing a checked in the checklistbox based on the value in database table. 3 46
Why use this lambda? 12 65
How to repeat the data 4 30
Set custom font WPF 2 21
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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