Specified cast is not valid.

A

Arjen

Hello,

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.
Line 176.

Source Error:


Line 174:
Line 175:
Line 176: DataRowView drv = (DataRowView) e.Item.DataItem;
Line 177:
Line 178: System.Web.UI.WebControls.Image thumb =
(System.Web.UI.WebControls.Image) e.Item.FindControl("thumbnail");


Can somebody help me?

Thanks!
 
F

Frank Drebin

You're casting a "DataItem" as a "DataRowView" - that's an invalid cast!!
hehehe - I know I sound like your error, but it's true. You can't pretend a
DataItem is a DataRowView..
 
A

Arjen

And now?





Frank Drebin said:
You're casting a "DataItem" as a "DataRowView" - that's an invalid cast!!
hehehe - I know I sound like your error, but it's true. You can't pretend a
DataItem is a DataRowView..
 
F

Frank Drebin

And now, you still can't. You can only coerce a variable to pretend it's a
different type - when they are close enough..

In the same way you couldn't do this:

TreeView objMyTree = (TreeView)strSomeString;

You can't cast things that are vastly different. There is no way to coerce a
string into representing a TreeView.
 
M

MS News \(MS ILM\)

Put a breaking point at DataRowView drv = (DataRowView) e.Item.DataItem;
Then Do a Quick Watch on e.item.DataItem or find the type of e.item.DataItem
and see what type it is
Then cast to that type
 
A

Arjen

Here is the complete function:
private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e) {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {

DataRowView drv = (DataRowView) e.Item.DataItem;

System.Web.UI.WebControls.Image thumb = (System.Web.UI.WebControls.Image)
e.Item.FindControl("thumbnail");
thumb.Attributes.Add("OnClick", "SelectImage('" + drv["itemId"].ToString()
+ "')");

}
}



May be this helps.
 
F

Frank Drebin

I *think* in that scenario - that e is actually your DataRowView..

Set a breakpoint and add a watch to 'e' and see what it is, if it is a
DataRowView then you can just reference things as:

e["itemId"].ToString()

hth

Arjen said:
Here is the complete function:
private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e) {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {

DataRowView drv = (DataRowView) e.Item.DataItem;

System.Web.UI.WebControls.Image thumb = (System.Web.UI.WebControls.Image)
e.Item.FindControl("thumbnail");
thumb.Attributes.Add("OnClick", "SelectImage('" + drv["itemId"].ToString()
+ "')");

}
}



May be this helps.













MS News (MS ILM) said:
Put a breaking point at DataRowView drv = (DataRowView) e.Item.DataItem;
Then Do a Quick Watch on e.item.DataItem or find the type of e.item.DataItem
and see what type it is
Then cast to that type


of
the
 
K

Kevin Spencer

I *think* in that scenario - that e is actually your DataRowView.

"e" is an instance of DataGridItemEventArgs. "e.Item" is the member to look
at. It can be one of several possible types, such as a heading section, a
footer section, or a data row. However, setting a breakpoint and doing a
watch should indeed revela what e.Item is.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.

Frank Drebin said:
I *think* in that scenario - that e is actually your DataRowView..

Set a breakpoint and add a watch to 'e' and see what it is, if it is a
DataRowView then you can just reference things as:

e["itemId"].ToString()

hth

Arjen said:
Here is the complete function:
private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e) {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {

DataRowView drv = (DataRowView) e.Item.DataItem;

System.Web.UI.WebControls.Image thumb = (System.Web.UI.WebControls.Image)
e.Item.FindControl("thumbnail");
thumb.Attributes.Add("OnClick", "SelectImage('" + drv["itemId"].ToString()
+ "')");

}
}



May be this helps.













MS News (MS ILM) said:
Put a breaking point at DataRowView drv = (DataRowView) e.Item.DataItem;
Then Do a Quick Watch on e.item.DataItem or find the type of e.item.DataItem
and see what type it is
Then cast to that type


And now?





"Frank Drebin" <[email protected]> schreef in bericht
You're casting a "DataItem" as a "DataRowView" - that's an invalid
cast!!
hehehe - I know I sound like your error, but it's true. You can't
pretend
a
DataItem is a DataRowView..

Hello,

Description: An unhandled exception occurred during the
execution
of is
not
 
M

MS News \(MS ILM\)

string thetype = e.Item.DataItem.GetType();

What do you get


Arjen said:
When I change it to e["itemId"].ToString().
VS says:
Cannot apply indexing with [] to an expression of type
'System.Web.UI.WebControls.DataGridItemEventArgs'

The debug says:
object DataGridItem.DateItem
Gets or sets the data item represented by the
System.Web.UI.WebControls.DataGridItem object in the
System.Web.UI.WebControls.DataGrid

I don't know what to do now... you?
Thanks!











Frank Drebin said:
I *think* in that scenario - that e is actually your DataRowView..

Set a breakpoint and add a watch to 'e' and see what it is, if it is a
DataRowView then you can just reference things as:

e["itemId"].ToString()

hth

Arjen said:
Here is the complete function:
private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e) {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {

DataRowView drv = (DataRowView) e.Item.DataItem;

System.Web.UI.WebControls.Image thumb = (System.Web.UI.WebControls.Image)
e.Item.FindControl("thumbnail");
thumb.Attributes.Add("OnClick", "SelectImage('" + drv["itemId"].ToString()
+ "')");

}
}



May be this helps.













"MS News (MS ILM)" <[email protected]> schreef in bericht
Put a breaking point at DataRowView drv = (DataRowView)
e.Item.DataItem;
Then Do a Quick Watch on e.item.DataItem or find the type of
e.item.DataItem
and see what type it is
Then cast to that type


And now?





"Frank Drebin" <[email protected]> schreef in bericht
You're casting a "DataItem" as a "DataRowView" - that's an invalid
cast!!
hehehe - I know I sound like your error, but it's true. You can't
pretend
a
DataItem is a DataRowView..

Hello,

Description: An unhandled exception occurred during the
execution
of
the
current web request. Please review the stack trace for more
information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is
not
valid.
Line 176.

Source Error:


Line 174:
Line 175:
Line 176: DataRowView drv = (DataRowView) e.Item.DataItem;
Line 177:
Line 178: System.Web.UI.WebControls.Image thumb =
(System.Web.UI.WebControls.Image) e.Item.FindControl("thumbnail");


Can somebody help me?

Thanks!
 
C

Carl Prothman [MVP]

Arjen said:
I use now a datagrid and the IBS a datalist.

Some body else a solution?

Arjen,
If you use a DataSet to bind the DataList, then that is the correct syntax.

However, if you use a SqlDataReader to bind, then you must cast the
DataItem to a System.Data.Common.DbDataRecord.
e.g.
System.Data.Common.DbDataRecord dbDataRecord =
(System.Data.Common.DbDataRecord) e.Item.DataItem;
String s = dbDataRecord.GetString(0);

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
 
K

Kevin Spencer

I said look at the e.Item property. It's not a Collection.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.

Arjen said:
When I change it to e["itemId"].ToString().
VS says:
Cannot apply indexing with [] to an expression of type
'System.Web.UI.WebControls.DataGridItemEventArgs'

The debug says:
object DataGridItem.DateItem
Gets or sets the data item represented by the
System.Web.UI.WebControls.DataGridItem object in the
System.Web.UI.WebControls.DataGrid

I don't know what to do now... you?
Thanks!











Frank Drebin said:
I *think* in that scenario - that e is actually your DataRowView..

Set a breakpoint and add a watch to 'e' and see what it is, if it is a
DataRowView then you can just reference things as:

e["itemId"].ToString()

hth

Arjen said:
Here is the complete function:
private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e) {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) {

DataRowView drv = (DataRowView) e.Item.DataItem;

System.Web.UI.WebControls.Image thumb = (System.Web.UI.WebControls.Image)
e.Item.FindControl("thumbnail");
thumb.Attributes.Add("OnClick", "SelectImage('" + drv["itemId"].ToString()
+ "')");

}
}



May be this helps.













"MS News (MS ILM)" <[email protected]> schreef in bericht
Put a breaking point at DataRowView drv = (DataRowView)
e.Item.DataItem;
Then Do a Quick Watch on e.item.DataItem or find the type of
e.item.DataItem
and see what type it is
Then cast to that type


And now?





"Frank Drebin" <[email protected]> schreef in bericht
You're casting a "DataItem" as a "DataRowView" - that's an invalid
cast!!
hehehe - I know I sound like your error, but it's true. You can't
pretend
a
DataItem is a DataRowView..

Hello,

Description: An unhandled exception occurred during the
execution
of
the
current web request. Please review the stack trace for more
information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is
not
valid.
Line 176.

Source Error:


Line 174:
Line 175:
Line 176: DataRowView drv = (DataRowView) e.Item.DataItem;
Line 177:
Line 178: System.Web.UI.WebControls.Image thumb =
(System.Web.UI.WebControls.Image) e.Item.FindControl("thumbnail");


Can somebody help me?

Thanks!
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top