Solved

Help finding row in datatable

Posted on 2007-04-07
6
331 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

8 Experts available now in Live!

Get 1:1 Help Now