Solved

Help finding row in datatable

Posted on 2007-04-07
6
333 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server Error 11 48
how to read json value 2 32
Eagerly loading related objects in entity framework 5 26
C# Comparing String Arrays. 5 28
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now