ItemDataBound problem

Discussion in 'ASP .Net Web Controls' started by Gsnke, Jan 7, 2004.

  1. Gsnke

    Gsnke Guest

    ---------------------------------
    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.FindControl("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???
    Gsnke, Jan 7, 2004
    #1
    1. Advertising

  2. Gsnke

    Alvin Bruney Guest

    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" <> wrote in message
    news:%...
    > ---------------------------------
    > 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.FindControl("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???
    >
    >
    Alvin Bruney, Jan 7, 2004
    #2
    1. Advertising

  3. Gsnke

    Gsnke Guest

    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:...
    > 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" <> wrote in message
    > news:%...
    Gsnke, Jan 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriël
    Replies:
    1
    Views:
    289
    Gabriël
    May 27, 2004
  2. Girish
    Replies:
    4
    Views:
    667
    Patrick.O.Ige
    Jul 19, 2005
  3. Nathan Sokalski

    Problem with DataList's ItemDataBound Event

    Nathan Sokalski, May 25, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    566
    Eliyahu Goldin
    May 27, 2007
  4. Rabii
    Replies:
    0
    Views:
    125
    Rabii
    Sep 12, 2004
  5. Bill Yeager

    Problem with ItemDAtaBound event of datalist

    Bill Yeager, Oct 25, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    133
    bill yeager
    Oct 26, 2004
Loading...

Share This Page