Specified cast is not valid.

  • Thread starter Thread starter Arjen
  • Start date Start date
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!
 
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..
 
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..
 
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.
 
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
 
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.
 
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
 
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
 
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!
 
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
 
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!
 
Back
Top