I am having a bit of trouble understanding something. I have a gridview which is populated from an ObjectDataSource. The ObjectDataSource is populated from a business object that I have written and this works fine. The gridview displays my data fine, the
business object is "TrlUtil.GroupTreeItem"
In the RowDataBound event of my Gridview I am trying to access the data item for the row I am currently processing, when using an SQL data source I would access the data item in the following way:
Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
However, ASP.Net does not like this as the data item is not a data row view and it says to me that I must cast this data item to the type of TrlUtil.GroupTreeItem. I understand this, it makes perfect sense, however when I do it ASP.Net comes up with this
error message and does it on all of my forms that try to read from a business object:
[A]GroupTreeItem cannot be cast to [B]GroupTreeItem. Type A originates from 'App_Code.uqb3lpsm, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Users\Craig\AppData\Local\Temp\Temporary ASP.NET Files\root\aeaecff4\a15625fb\App_Code.uqb3lpsm.dll'.
Type B originates from '[AppNameRemovedForSecurity], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Users\Craig\AppData\Local\Temp\Temporary ASP.NET Files\root\aeaecff4\a15625fb\assembly\dl3\f0c52524\8b739638_5fd5cd01\[AppNameRemovedForSecurity].DLL'.
Does anyone know why this may be happening please?
The e.Row.DataItem is actually of type GroupTreeItem and the cast I am trying to make is correct. Below is what I am having to do at present to get around the problem
Dim gti = e.Row.DataItem ' dataitem is of type TrlUtil.GroupTypeItem.
By not casting gti to any particular type I can use it as I would a GroupTreeItem (so I can use terms such as gti.Id and gti.GroupName) and any attempt to read the datatype of gti will return [GroupTreeItem] however ASP will not allow a cast to type [GroupTreeItem]
as it sees it being of a different data type.
I have Googled the problem and there are others that are having the same issue and it appears to be due to ASP generating a circular reference when it tries to compile the project. Here is a link to another post by someone having a similar problem:
But there was never an finaly answer to the problem. Someone else wrote a post later down the page with the word SOLVED! but the solution doesn't work and was never accepted as an answer by the original poster.
Does anyone know a work around to this other than my current method which is late binding?
Dim gti = e.Row.DataItem ' dataitem is of type TrlUtil.GroupTypeItem.
Hello again,
GridView has many rows, each row is called an GridViewRow. And each Row has a property called DataItem, which maps the type of your boundable Database source. So your type is "TrlUtil.GroupTypeItem".
And it has nothing to do with DataRow at all. So you should convert it to a proper class.
I believe I understand. As I get it the row's dataitem maps to the class that my business object is based on which means that e.Row.DataItem should map to TrlUtil.GroupTypeItem.
This means that the following lines should work:
Dim gti as TrlUtil.GroupTypeItem = e.Row.DataItem
or
Dim gti as TrlUtil.GroupTypeItem = CType(e.Row.DataItem, TrlUtil.GroupTypeItem)
Is this what you are saying or are we on different wavelengths?
UselessChimp
Member
210 Points
110 Posts
Casting Gridview.DataItem to BusinessObject
Dec 08, 2012 03:23 PM|LINK
Hello all,
I am having a bit of trouble understanding something. I have a gridview which is populated from an ObjectDataSource. The ObjectDataSource is populated from a business object that I have written and this works fine. The gridview displays my data fine, the business object is "TrlUtil.GroupTreeItem"
In the RowDataBound event of my Gridview I am trying to access the data item for the row I am currently processing, when using an SQL data source I would access the data item in the following way:
Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
However, ASP.Net does not like this as the data item is not a data row view and it says to me that I must cast this data item to the type of TrlUtil.GroupTreeItem. I understand this, it makes perfect sense, however when I do it ASP.Net comes up with this error message and does it on all of my forms that try to read from a business object:
[A]GroupTreeItem cannot be cast to [B]GroupTreeItem. Type A originates from 'App_Code.uqb3lpsm, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Users\Craig\AppData\Local\Temp\Temporary ASP.NET Files\root\aeaecff4\a15625fb\App_Code.uqb3lpsm.dll'. Type B originates from '[AppNameRemovedForSecurity], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Users\Craig\AppData\Local\Temp\Temporary ASP.NET Files\root\aeaecff4\a15625fb\assembly\dl3\f0c52524\8b739638_5fd5cd01\[AppNameRemovedForSecurity].DLL'.
Does anyone know why this may be happening please?
oned_gk
All-Star
31507 Points
6429 Posts
Re: Casting Gridview.DataItem to BusinessObject
Dec 08, 2012 11:47 PM|LINK
or try
or
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Casting Gridview.DataItem to BusinessObject
Dec 09, 2012 05:49 AM|LINK
Hello,
Please use Response.Write to output the real type of e.Row.DataItem,and then use that type be instead of DataRowView.
Reguards!
UselessChimp
Member
210 Points
110 Posts
Re: Casting Gridview.DataItem to BusinessObject
Dec 09, 2012 03:18 PM|LINK
Hello and thank you both for your replies.
The e.Row.DataItem is actually of type GroupTreeItem and the cast I am trying to make is correct. Below is what I am having to do at present to get around the problem
Dim gti = e.Row.DataItem ' dataitem is of type TrlUtil.GroupTypeItem.
By not casting gti to any particular type I can use it as I would a GroupTreeItem (so I can use terms such as gti.Id and gti.GroupName) and any attempt to read the datatype of gti will return [GroupTreeItem] however ASP will not allow a cast to type [GroupTreeItem] as it sees it being of a different data type.
I have Googled the problem and there are others that are having the same issue and it appears to be due to ASP generating a circular reference when it tries to compile the project. Here is a link to another post by someone having a similar problem:
http://stackoverflow.com/questions/6797896/cannot-be-cast-to-b-same-context-default-different-temp-file
But there was never an finaly answer to the problem. Someone else wrote a post later down the page with the word SOLVED! but the solution doesn't work and was never accepted as an answer by the original poster.
Does anyone know a work around to this other than my current method which is late binding?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Casting Gridview.DataItem to BusinessObject
Dec 10, 2012 01:05 AM|LINK
Hello again,
GridView has many rows, each row is called an GridViewRow. And each Row has a property called DataItem, which maps the type of your boundable Database source. So your type is "TrlUtil.GroupTypeItem".
And it has nothing to do with DataRow at all. So you should convert it to a proper class.
UselessChimp
Member
210 Points
110 Posts
Re: Casting Gridview.DataItem to BusinessObject
Dec 10, 2012 08:13 AM|LINK
Hello mate,
I believe I understand. As I get it the row's dataitem maps to the class that my business object is based on which means that e.Row.DataItem should map to TrlUtil.GroupTypeItem.
This means that the following lines should work:
Dim gti as TrlUtil.GroupTypeItem = e.Row.DataItem
or
Dim gti as TrlUtil.GroupTypeItem = CType(e.Row.DataItem, TrlUtil.GroupTypeItem)
Is this what you are saying or are we on different wavelengths?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Casting Gridview.DataItem to BusinessObject
Dec 11, 2012 12:10 AM|LINK
Yes, at least I think so……You mean they still don't work?
Do you have the same class name but in different kind of namespace?