Professional Web Applications Themes

Specified cast is not valid. - ASP.NET General

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!...

  1. #1

    Default 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!


    Arjen Guest

  2. #2

    Default Re: Specified cast is not valid.

    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" <com> wrote in message
    news:bhtlmp$pm5$tilbu1.nb.home.nl... 
    valid. 


    Frank Guest

  3. #3

    Default Re: Specified cast is not valid.

    And now?





    "Frank Drebin" <com> schreef in bericht
    news:eUs0b.27306$news.prodigy.com... 

    > valid. 
    >
    >[/ref]


    Arjen Guest

  4. #4

    Default Re: Specified cast is not valid.


    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" <com> wrote in message
    news:bhtm1o$1rf$tilbu1.nb.home.nl... [/ref]
    cast!! [/ref]
    pretend [/ref][/ref]
    the [/ref][/ref]
    information 
    > > valid. 
    > >
    > >[/ref]
    >
    >[/ref]


    Frank Guest

  5. #5

    Default Re: Specified cast is not valid.

    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" <com> wrote in message
    news:bhtm1o$1rf$tilbu1.nb.home.nl... [/ref]
    cast!! [/ref]
    pretend [/ref][/ref]
    the [/ref][/ref]
    information 
    > > valid. 
    > >
    > >[/ref]
    >
    >[/ref]


    MS Guest

  6. #6

    Default Re: Specified cast is not valid.

    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)" <com> schreef in bericht
    news:phx.gbl... 
    e.Item.DataItem; 
    e.item.DataItem [/ref]
    > cast!! [/ref]
    > pretend [/ref]
    > the [/ref]
    > information [/ref][/ref]
    not 
    > >
    > >[/ref]
    >
    >[/ref]


    Arjen Guest

  7. #7

    Default Re: Specified cast is not valid.

    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" <com> wrote in message
    news:bhtn9p$uve$tilbu1.nb.home.nl... 
    (System.Web.UI.WebControls.Image) 
    drv["itemId"].ToString() 
    > e.Item.DataItem; 
    > e.item.DataItem 
    > > cast!! 
    > > pretend [/ref][/ref]
    of 
    > > information [/ref]
    > not 
    > >
    > >[/ref]
    >
    >[/ref]


    Frank Guest

  8. #8

    Default Re: Specified cast is not valid.

    > 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" <com> wrote in message
    news:sjt0b.27315$news.prodigy.com... 
    > (System.Web.UI.WebControls.Image) 
    > drv["itemId"].ToString() 
    > > e.Item.DataItem; 
    > > e.item.DataItem [/ref][/ref]
    execution [/ref][/ref]
    is [/ref][/ref]
    e.Item.FindControl("thumbnail"); 
    > >
    > >[/ref]
    >
    >[/ref]


    Kevin Guest

  9. #9

    Default Re: Specified cast is not valid.

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

    What do you get


    "Arjen" <com> wrote in message
    news:bhto6a$ook$tilbu1.nb.home.nl... 
    > > (System.Web.UI.WebControls.Image) 
    > > drv["itemId"].ToString() [/ref][/ref]
    invalid [/ref][/ref]
    can't [/ref]
    > execution [/ref]
    > is [/ref]
    > e.Item.FindControl("thumbnail"); 
    > >
    > >[/ref]
    >
    >[/ref]


    MS Guest

  10. #10

    Default Re: Specified cast is not valid.

    "Arjen" <com> wrote 
    > I use now a datagrid and the IBS a datalist.
    >
    > Some body else a solution?
    >[/ref]

    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



    Carl Guest

  11. #11

    Default Re: Specified cast is not valid.

    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" <com> wrote in message
    news:bhto6a$ook$tilbu1.nb.home.nl... 
    > > (System.Web.UI.WebControls.Image) 
    > > drv["itemId"].ToString() [/ref][/ref]
    invalid [/ref][/ref]
    can't [/ref]
    > execution [/ref]
    > is [/ref]
    > e.Item.FindControl("thumbnail"); 
    > >
    > >[/ref]
    >
    >[/ref]


    Kevin Guest

Similar Threads

  1. Specified Cast is not Valid
    By DJONES in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: June 20th, 05:56 PM
  2. Replies: 1
    Last Post: August 6th, 03:26 AM
  3. Please help on Specified cast is not valid exception
    By Jeffrey Sanchez in forum ASP.NET Web Services
    Replies: 0
    Last Post: August 4th, 04:12 PM
  4. Replies: 3
    Last Post: July 10th, 10:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139