473,473 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I get an error when trying to use foreach...

I get this error:

Unable to cast object of type 'System.Data.DataRowView' to type
'System.String'

on this foreach and I thought it should work:

foreach (string invoice in listBox2.Items)
{

}

Any help is appreciated.
Thanks,
Trint

Jul 6 '07 #1
8 2940
Try this:

foreach (System.Data.DataRowView invoice in listBox2.Items)
{
}

the listBox2.Items contains a collection of DataRowView not strings.
"trint" <tr***********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>I get this error:

Unable to cast object of type 'System.Data.DataRowView' to type
'System.String'

on this foreach and I thought it should work:

foreach (string invoice in listBox2.Items)
{

}

Any help is appreciated.
Thanks,
Trint

Jul 6 '07 #2
Rene wrote:
Try this:

foreach (System.Data.DataRowView invoice in listBox2.Items)
{
}

the listBox2.Items contains a collection of DataRowView not strings.
"trint" <tr***********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>>I get this error:

Unable to cast object of type 'System.Data.DataRowView' to type
'System.String'

on this foreach and I thought it should work:

foreach (string invoice in listBox2.Items)
{

}

Any help is appreciated.
Thanks,
Trint
Hi,

It might not. The items collection is a list of *objects*, which in turn
could be anything. Strings, DataRowViews, "other".

--
Tom Spink
University of Edinburgh
Jul 6 '07 #3
On Jul 6, 1:30 pm, Tom Spink <tsp...@gmail.comwrote:
Rene wrote:
Try this:
foreach (System.Data.DataRowView invoice in listBox2.Items)
{
}
the listBox2.Items contains a collection of DataRowView not strings.
"trint" <trinity.sm...@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
>I get this error:
Unable to cast object of type 'System.Data.DataRowView' to type
'System.String'
on this foreach and I thought it should work:
foreach (string invoice in listBox2.Items)
{
}
Any help is appreciated.
Thanks,
Trint

Hi,

It might not. The items collection is a list of *objects*, which in turn
could be anything. Strings, DataRowViews, "other".

--
Tom Spink
University of Edinburgh- Hide quoted text -

- Show quoted text -
How can I get my collection from listBox2 to a string so that I can
use this function, or is there another way to go through each of the
items in my listBox2 collection?

Thanks,
Trint

Jul 6 '07 #4
Well, the error indicates that is trying to cast a DataRowView to a string
so I can only assume that he is holding DataRowViews objects on it.

Now if there was no error description then yes, there would be no possible
way to know what to cast the object to.

"Tom Spink" <ts****@gmail.comwrote in message
news:uY**************@TK2MSFTNGP05.phx.gbl...
Rene wrote:
>Try this:

foreach (System.Data.DataRowView invoice in listBox2.Items)
{
}

the listBox2.Items contains a collection of DataRowView not strings.
"trint" <tr***********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googleg roups.com...
>>>I get this error:

Unable to cast object of type 'System.Data.DataRowView' to type
'System.String'

on this foreach and I thought it should work:

foreach (string invoice in listBox2.Items)
{

}

Any help is appreciated.
Thanks,
Trint

Hi,

It might not. The items collection is a list of *objects*, which in turn
could be anything. Strings, DataRowViews, "other".

--
Tom Spink
University of Edinburgh

Jul 6 '07 #5
trint wrote:
On Jul 6, 1:30 pm, Tom Spink <tsp...@gmail.comwrote:
>Rene wrote:
Try this:
foreach (System.Data.DataRowView invoice in listBox2.Items)
{
}
the listBox2.Items contains a collection of DataRowView not strings.
"trint" <trinity.sm...@gmail.comwrote in message
news:11**********************@q75g2000hsh.googleg roups.com...
I get this error:
>Unable to cast object of type 'System.Data.DataRowView' to type
'System.String'
>on this foreach and I thought it should work:
>foreach (string invoice in listBox2.Items)
{
>}
>Any help is appreciated.
Thanks,
Trint

Hi,

It might not. The items collection is a list of *objects*, which in turn
could be anything. Strings, DataRowViews, "other".

--
Tom Spink
University of Edinburgh- Hide quoted text -

- Show quoted text -

How can I get my collection from listBox2 to a string so that I can
use this function, or is there another way to go through each of the
items in my listBox2 collection?

Thanks,
Trint
Well, if you modify your foreach-loop like this:

///
foreach (object invoice in listBox2.Items) {
// You can use invoice.ToString() to get a
// string representation.
}
///

Calling the ToString method will try and get you a string representation of
the object.
--
Tom Spink
University of Edinburgh
Jul 6 '07 #6
Rene wrote:
Well, the error indicates that is trying to cast a DataRowView to a string
so I can only assume that he is holding DataRowViews objects on it.

Now if there was no error description then yes, there would be no possible
way to know what to cast the object to.

"Tom Spink" <ts****@gmail.comwrote in message
news:uY**************@TK2MSFTNGP05.phx.gbl...
>Rene wrote:
>>Try this:

foreach (System.Data.DataRowView invoice in listBox2.Items)
{
}

the listBox2.Items contains a collection of DataRowView not strings.
"trint" <tr***********@gmail.comwrote in message
news:11**********************@q75g2000hsh.google groups.com...
I get this error:

Unable to cast object of type 'System.Data.DataRowView' to type
'System.String'

on this foreach and I thought it should work:

foreach (string invoice in listBox2.Items)
{

}

Any help is appreciated.
Thanks,
Trint

Hi,

It might not. The items collection is a list of *objects*, which in turn
could be anything. Strings, DataRowViews, "other".

--
Tom Spink
University of Edinburgh
Indeed, but what I meant was it can contain a mixture of these objects as
well, so some of them might be DataRowViews and some of them might be boxed
integers, and others might be strings or even System.Threads.

But, that was slightly nit-picky of me, because you're probably right; it'll
contain just DataRowViews.

--
Tom Spink
University of Edinburgh
Jul 6 '07 #7
Indeed, that was slightly nit-picky of you :)

The focus was simply to point out that there was no implicit conversion from
the collection object to the string object.
Indeed, but what I meant was it can contain a mixture of these objects as
well, so some of them might be DataRowViews and some of them might be
boxed
integers, and others might be strings or even System.Threads.

But, that was slightly nit-picky of me, because you're probably right;
it'll
contain just DataRowViews.

--
Tom Spink
University of Edinburgh

Jul 6 '07 #8
Rene <a@b.comwrote:
Indeed, that was slightly nit-picky of you :)

The focus was simply to point out that there was no implicit conversion from
the collection object to the string object.
Note that foreach doesn't require an implicit conversion to be
available at compile-time - it requires that a cast would work at
execution time (and that a cast is *feasible* at compile-time). For
instance:

object x = new object[]{"x", "y", "z"};

foreach (string s in x)
{
Console.WriteLine(s);
}

That will work, despite there being no implicit conversion from object
to string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 7 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
4
by: David Moore | last post by:
Hello I am using the System.DirectoryServices namespace classes to access Active Directory. We connect using the LDAP://DOMAIN method. The code works on local dev boxes, and in staging, but...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
9
by: Shuan | last post by:
I am getting this error. Can it be fixed by setting more than 60 for the max_execution_time in php.in file? Fatal error: Maximum execution time of 60 seconds exceeded in categorycrawler.php on...
5
by: ismail.mayat | last post by:
Hello, I am having a problem with Activator.CreateInstance. I have the following code Assembly asm = System.Reflection.Assembly.LoadFrom(assembly); if(asm!=null) { try {
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: Stephen Thomas | last post by:
Hi there I wonder if any one has encountered this problem or can suggest what is wrong. I am trying the a procedure from the msn site but get the following message: Error 1 The type or...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.