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

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

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
 
Try this:

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

the listBox2.Items contains a collection of DataRowView not strings.
 
Rene said:
Try this:

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

the listBox2.Items contains a collection of DataRowView not strings.

Hi,

It might not. The items collection is a list of *objects*, which in turn
could be anything. Strings, DataRowViews, "other".
 
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, 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.
 
trint said:
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.
 
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- Hide quoted text -

- Show quoted text -

Well, here is my code that I need to work like it once did (I don't
know but I think it was just the change from dotNet version 1.1 to
2.0:

foreach (string invoice in listBox2.Items)
{
PrintReporttoLaserjet pe = new
PrintReporttoLaserjet("InvoiceNumber", invoice, new string[] { @"/
orders1",@"/orders1",@"/orders1" });
pe.PrintReport(@"HP PSC 2350 series", new string[]
{ ""});

}
Thanks for any help.
Trint
 
trint said:
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- Hide quoted text -

- Show quoted text -

Well, here is my code that I need to work like it once did (I don't
know but I think it was just the change from dotNet version 1.1 to
2.0:

foreach (string invoice in listBox2.Items)
{
PrintReporttoLaserjet pe = new
PrintReporttoLaserjet("InvoiceNumber", invoice, new string[] { @"/
orders1",@"/orders1",@"/orders1" });
pe.PrintReport(@"HP PSC 2350 series", new string[]
{ ""});

}
Thanks for any help.
Trint

Well, did you try what I suggested? Changing the foreach to iterate over
*object invoice* instead of *string invoice*, then using the .ToString
method to get a string representation?

So, your foreach line will read:
foreach (object invoice in listBox2.Items)

And on your print line, change 'invoice' to 'invoice.ToString()'.
 
Rene said:
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.

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.
 
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.
 
Rene said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top