Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help finding row in datatable

Posted on 2007-04-07
6
338 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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