Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > ItemDataBound problem

Reply
Thread Tools

ItemDataBound problem

 
 
Gsnke
Guest
Posts: n/a
 
      01-07-2004
---------------------------------
I'm getting this error:

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.

Source Error:


Line 67: if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
Line 68: {
Line 69: string sImageID =
(((System.Data.Common.DbDataRecord)e.Item.DataItem )["ImageID"]).ToString();
Line 70: if(sImageID.Length < 1)
Line 71: {

---------------------------------
with this code:

private void rptStoryList_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string sImageID =
(((System.Data.Common.DbDataRecord)e.Item.DataItem )["ImageID"]).ToString();
if(sImageID.Length < 1)
{
((System.Web.UI.WebControls.Image)e.Item.FindContr ol("imgIT")).Visible
= false;
}
}
}
----------------------------------
I'm trying to hide an <asp:image control if no image id comes out of the
database. And I'm pretty sure this problem has to do with NULL values coming
out. How can I check for that???


 
Reply With Quote
 
 
 
 
Alvin Bruney
Guest
Posts: n/a
 
      01-07-2004
no it's not. it is expecting one type, and another type is coming back so it
doesn't know what to do and falls over dead. a cursory examination of the
code reveals to me that find controls returns a type control. you need to
then take the control object and cast it to suitable type like image or
whatever it is you are after. The preceeding only fails if you have a
template column. I can't tell what you have. in that case, your code would
be correct because the appropriate type would be returned without the need
to cast.

wait a minute...
>(((System.Data.Common.DbDataRecord)

that's not a control. you can't cast to that. so this is the problem. i'll
use a label control to explain. If you did a findcontrol("mycontrol") it
would return a control of type label assuming your template column has label
controls in it. then you extract the info out of the label
ex:
Label t = (Label)e.item.FindControl("mycontrol");
if(lbl != null)
t.text = "new text here";

make sense?

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/2bz4t
"Gsnke" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> ---------------------------------
> I'm getting this error:
>
> 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.
>
> Source Error:
>
>
> Line 67: if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem)
> Line 68: {
> Line 69: string sImageID =
>

(((System.Data.Common.DbDataRecord)e.Item.DataItem )["ImageID"]).ToString();
> Line 70: if(sImageID.Length < 1)
> Line 71: {
>
> ---------------------------------
> with this code:
>
> private void rptStoryList_ItemDataBound(object sender,
> System.Web.UI.WebControls.RepeaterItemEventArgs e)
> {
> if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem)
> {
> string sImageID =
>

(((System.Data.Common.DbDataRecord)e.Item.DataItem )["ImageID"]).ToString();
> if(sImageID.Length < 1)
> {
>

((System.Web.UI.WebControls.Image)e.Item.FindContr ol("imgIT")).Visible
> = false;
> }
> }
> }
> ----------------------------------
> I'm trying to hide an <asp:image control if no image id comes out of the
> database. And I'm pretty sure this problem has to do with NULL values

coming
> out. How can I check for that???
>
>



 
Reply With Quote
 
 
 
 
Gsnke
Guest
Posts: n/a
 
      01-07-2004
Ok, how would I then grab the data ("ImageID") from the datasource and
evaluate whether to show that image?

Thanks


"Alvin Bruney" <vapor at steaming post office> wrote in message
news:(E-Mail Removed)...
> no it's not. it is expecting one type, and another type is coming back so

it
> doesn't know what to do and falls over dead. a cursory examination of the
> code reveals to me that find controls returns a type control. you need to
> then take the control object and cast it to suitable type like image or
> whatever it is you are after. The preceeding only fails if you have a
> template column. I can't tell what you have. in that case, your code would
> be correct because the appropriate type would be returned without the need
> to cast.
>
> wait a minute...
> >(((System.Data.Common.DbDataRecord)

> that's not a control. you can't cast to that. so this is the problem. i'll
> use a label control to explain. If you did a findcontrol("mycontrol") it
> would return a control of type label assuming your template column has

label
> controls in it. then you extract the info out of the label
> ex:
> Label t = (Label)e.item.FindControl("mycontrol");
> if(lbl != null)
> t.text = "new text here";
>
> make sense?
>
> --
> Regards,
> Alvin Bruney
> Got tidbits? Get it here...
> http://tinyurl.com/2bz4t
> "Gsnke" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with DataList's ItemDataBound Event Nathan Sokalski ASP .Net 2 05-27-2007 07:44 AM
Grid within a Grid ItemDataBound event problem Girish ASP .Net 4 07-19-2005 01:17 AM
Problem with ItemDAtaBound event of datalist Bill Yeager ASP .Net Datagrid Control 1 10-26-2004 02:28 PM
Removing a row in each ItemDataBound (with condition) results in a paging problem Rabii ASP .Net Datagrid Control 0 09-12-2004 08:33 PM
Problem with ItemDataBound / ItemCreate (text seems to be empty) GabriŽl ASP .Net 1 05-27-2004 04:55 PM



Advertisments