Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cannot convert type 'char' to 'System.Data.DataRow'

Posted on 2004-10-25
11
Medium Priority
?
1,198 Views
Last Modified: 2012-06-27
is the message i receive when attempting to compile the following statement:

foreach(System.Data.DataRow dr in myDataSet.Tables[0].DefaultView.RowFilter ="Status_FK = 1")

Basically, I'd like to use an existing dataset, apply the rowfilter, and then iterate through the filtered rows.

0
Comment
Question by:brdrok
[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
11 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 12401317
I guess it should be something like this:

myDataSet.Tables[0].DefaultView.RowFilter ="Status_FK = 1";

foreach(System.Data.DataRow dr in myDataSet.Tables[0].DefaultView)
{
    ....
}



0
 
LVL 7

Author Comment

by:brdrok
ID: 12402640
Thanks unfortunately i get an error message that says:

Specified cast is not valid....

wish i could get a better error description....

0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12402889
I think it should be corrected a little bit to:

myDataSet.Tables[0].DefaultView.RowFilter ="Status_FK = 1";

foreach(System.Data.DataRow dr in myDataSet.Tables[0].DefaultView.Table.Rows)
{
   ....
}

does that work?
0
Industry Leaders: 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!

 
LVL 7

Author Comment

by:brdrok
ID: 12403166
Hey Razzie,

I think the suggestion of yours is very close....at least I stopped getting the error message, however, the foreach statement seems to go through every single datarow and not the filtered rows.  so i wrote a couple messagebox functions that shows me the number of records and here is that i came up with

MessageBox.Show(myDataSet.Tables[0].DefaultView.Count.ToString());   <--returns 12, which sounds about right

MessageBox.Show(myDataSet.Tables[0].DefaultView.Table.Rows.Count.ToString()); <--returns 118, which are all the records

foreach(System.Data.DataRow dr in myDataSet.Tables[0].DefaultView.Table.Rows)
{
    //goes through all 118 records....
    ..........................
}

time for me to buy a good ado.net book :)


0
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 12403266
If the view being filtered isn't really important for anything else, then use the DataTable.Select command instead:

foreach(System.Data.DataRow dr in myDataSet.Tables[0].Select("Status_FK=1"))
{
}

This will return a collection of datarows matching the criteria which you can iterate through.
0
 
LVL 7

Author Comment

by:brdrok
ID: 12403389
Snarf0001:

thanks...works really well right now.  If you don't mind explaining in layman's term...what exactly is the difference between the RowFilter and the "Select" methods?  

thanks

0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 12403463
No problem.

The rowfilter is used on the dataview, which generally is used for filtering on bound controls, ie eliminating rows from a bound drop down or something like that.  Dataviews CAN be used for processing data like you want to, but it's more of a pain, as that's not really what they were designed to do.

The .select will do basically the same thing as the rowfilter, but it's performed on the actual datatable itself, not the dataview, and will return an array of datarows for processing or whatever you might want.

Primarily, rowfilter acts on the view, and is used for changing display, .select acts on the table, returns an external set for processing.
0
 
LVL 7

Author Comment

by:brdrok
ID: 12403875
I think I understand....basically when using the .select...we are making another call to the database....but when using the rowfilter...the processing is being done on the cpu and no call to database is being made...

0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 12403922
no no, the select is not going back to the db.  The datatable itself is still in memory in the app, with all of it's datarows.  Each one can have multiple dataviews, which just play around with the data in memory in the datatable.
A select statement, will just make a collection out of certain rows in memory.  Still no trip to the db.
The datatable is the only thing that actually HAS the data, the view just manipulates it, and a .select just exposes some of it.
0
 
LVL 7

Author Comment

by:brdrok
ID: 12404111
gotcha....those are some real knowledge gold nuggest today :)

is there a particular syntax that will release the collection of certain rows in memory or should i leave that up to the garbage collector?

too bad i already closed the question or else i could have added a few more points or uploaded a six pack of beer...

0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 12404175
lol

Leave the rows up to the garbage collector.  They're still actually attached to the datatable, but I wouldn't really worry about that at this point.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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