Solved

Help finding row in datatable

Posted on 2007-04-07
6
340 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
[X]
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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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