468,256 Members | 1,451 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

Specified cast is not valid.

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!
Nov 17 '05 #1
10 1553
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..

"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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!

Nov 17 '05 #2
And now?

"Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
news:eU**********************@newssvr28.news.prodi gy.com...
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..

"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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!


Nov 17 '05 #3

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.

"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
And now?

"Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
news:eU**********************@newssvr28.news.prodi gy.com...
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..

"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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!



Nov 17 '05 #4
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
"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
And now?

"Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
news:eU**********************@newssvr28.news.prodi gy.com...
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..

"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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!



Nov 17 '05 #5
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)" <sq**********@hotmail.com> schreef in bericht
news:up**************@TK2MSFTNGP09.phx.gbl...
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
"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
And now?

"Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
news:eU**********************@newssvr28.news.prodi gy.com...
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..

"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
> 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!
>
>



Nov 17 '05 #6
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" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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)" <sq**********@hotmail.com> schreef in bericht
news:up**************@TK2MSFTNGP09.phx.gbl...
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
"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
And now?

"Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
news:eU**********************@newssvr28.news.prodi gy.com...
> 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..
>
> "Arjen" <bo*****@hotmail.com> wrote in message
> news:bh**********@news1.tilbu1.nb.home.nl...
> > 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!
> >
> >
>
>



Nov 17 '05 #7
string thetype = e.Item.DataItem.GetType();

What do you get
"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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" <no*****@imsickofspam.com> schreef in bericht
news:sj**********************@newssvr28.news.prodi gy.com...
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" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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)" <sq**********@hotmail.com> schreef in bericht
news:up**************@TK2MSFTNGP09.phx.gbl...
> 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
>
>
> "Arjen" <bo*****@hotmail.com> wrote in message
> news:bh**********@news1.tilbu1.nb.home.nl...
> > And now?
> >
> >
> >
> >
> >
> > "Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
> > news:eU**********************@newssvr28.news.prodi gy.com...
> > > 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..
> > >
> > > "Arjen" <bo*****@hotmail.com> wrote in message
> > > news:bh**********@news1.tilbu1.nb.home.nl...
> > > > 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!
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 17 '05 #8
> 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" <no*****@imsickofspam.com> wrote in message
news:sj**********************@newssvr28.news.prodi gy.com...
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" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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)" <sq**********@hotmail.com> schreef in bericht
news:up**************@TK2MSFTNGP09.phx.gbl...
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
"Arjen" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
> And now?
>
>
>
>
>
> "Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
> news:eU**********************@newssvr28.news.prodi gy.com...
> > 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..
> >
> > "Arjen" <bo*****@hotmail.com> wrote in message
> > news:bh**********@news1.tilbu1.nb.home.nl...
> > > 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!
> > >
> > >
> >
> >
>
>



Nov 17 '05 #9
"Arjen" <bo*****@hotmail.com> wrote
Line 176: DataRowView drv = (DataRowView) e.Item.DataItem;

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

Nov 17 '05 #10
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" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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" <no*****@imsickofspam.com> schreef in bericht
news:sj**********************@newssvr28.news.prodi gy.com...
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" <bo*****@hotmail.com> wrote in message
news:bh**********@news1.tilbu1.nb.home.nl...
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)" <sq**********@hotmail.com> schreef in bericht
news:up**************@TK2MSFTNGP09.phx.gbl...
> 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
>
>
> "Arjen" <bo*****@hotmail.com> wrote in message
> news:bh**********@news1.tilbu1.nb.home.nl...
> > And now?
> >
> >
> >
> >
> >
> > "Frank Drebin" <no*****@imsickofspam.com> schreef in bericht
> > news:eU**********************@newssvr28.news.prodi gy.com...
> > > 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..
> > >
> > > "Arjen" <bo*****@hotmail.com> wrote in message
> > > news:bh**********@news1.tilbu1.nb.home.nl...
> > > > 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!
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tyro | last post: by
3 posts views Thread by PK9 | last post: by
3 posts views Thread by VB Programmer | last post: by
reply views Thread by Alan Z. Scharf | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.