Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

System.InvalidCastException: Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'.

I'm not sure why I get this in my VB.NET 2.0 project.  In my DataGrid I have:

<asp:TemplateColumn HeaderText="Phone">
      <ItemStyle Wrap="False" HorizontalAlign="Left" CssClass="datagridcell" />
      <ItemTemplate>
            <nobr>
            <a  href="<%=GetClickToDialUrl%>?ClickToDial=81<%#StripPhone(CStr(CType(Container.DataItem, System.Data.DataRow)("Phone")))%>" target="ClickToDialFrame">
                        <%#FormatPhoneNumber(CStr(CType(Container.DataItem, System.Data.DataRow)("Phone")))%>
          </a>
            </nobr>
      </ItemTemplate>
</asp:TemplateColumn>

System.InvalidCastException: Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'.


Avatar of SimonBlake
SimonBlake

Any reason why you can't just use

<a  href="<%=GetClickToDialUrl%>?ClickToDial=81<%#StripPhone(

DataBinder.Eval(Container.DataItem, "PHONE")

)%>" target="ClickToDialFrame">

Sent as an object to StripPhone, which then casts it to a string?
Try to use "Container.DataItem.Row" replacing "Container.DataItem". Example:
...<a  href="<%=GetClickToDialUrl%>?ClickToDial=81<%#StripPhone(CStr(CType(Container.DataItem.Row, System.Data.DataRow)("Phone")))%>" target="ClickToDialFrame">
                        <%#FormatPhoneNumber(CStr(CType(Container.DataItem.Row, System.Data.DataRow)("Phone")))%>...
Avatar of dba123

ASKER

>>>Any reason why you can't just use
<a  href="<%=GetClickToDialUrl%>?ClickToDial=81<%#StripPhone(DataBinder.Eval(Container.DataItem, "PHONE"))%>" target="ClickToDialFrame">
Sent as an object to StripPhone, which then casts it to a string?

No, because OptionExplicit is required to be ON with .NET 2.0.  So if you were doing C#, this issue is one I would have already had workikng because C# requires this.  But since it's VB.NET I'm coming across this shit now.  So with Option Explicit On, no you can't do it that way.  And it's a good thing to have Option Explicit on.  You shouldn't turn that off.  Therefore I have to cast this crap like this.

Avatar of dba123

ASKER

can you tell I hate VB....yes.
ASKER CERTIFIED SOLUTION
Avatar of dba123
dba123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lol - def yes, altho, I haven't turned off explicit, and it worked in my code last night without issue which was 2.0
(I prefer c# as well.)
Avatar of dba123

ASKER

>>>Try to use "Container.DataItem.Row" replacing "Container.DataItem".

No, again same point

Option Strict On disallows late binding.

So that's a VB.NET thing.  If you were to code this in C#, you would not have these problems.
Doubt you can give yourself pts tho...
Happy to close with pts refund.
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator