Using Linq with Gridview

Posts   
 
    
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 04-Jun-2009 15:37:40   

dotnet3.5,llblgen 2.6,vb.net,asp.net

Trying to do some row manipulation but cannot retrieve the dataitem properties, the e.row.dataitem is returning an anonymous type instead of the VwTripDomesticCountEntity...does this mean i should avoid Linq and use the standard way to run queries?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then


        Using adapter As New DataAccessAdapter()
            Dim metaData As New LinqMetaData(adapter)
            Dim qDomestic = From p In metaData.VwTripDomesticCount _
                               Where p.FromCountryId = Integer.Parse(Session("YourCountryId")) _
                                Select p.FromStateId, p.FromState, p.Count

            GridViewDomestic.DataSource = qDomestic
            GridViewDomestic.DataBind()
        End Using

    End If
End Sub

Protected Sub GridViewDomestic_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewDomestic.RowDataBound
    Dim sRowHTML As String = "sdssssfsdfs"
    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim c As VwTripDomesticCountEntity = e.Row.DataItem


        sRowHTML = CType(e.Row.DataItem, VwTripDomesticCountEntity).FromCountry & CType(e.Row.DataItem, VwTripDomesticCountEntity).FromState 'NOT WORKING
        e.Row.Cells(1).Text = sRowHTML 
    End If

End Sub
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 04-Jun-2009 22:24:00   

When you say NOT WORKING, exactly what error are you getting...?

Could you try


Dim qDomestic = From p In metaData.VwTripDomesticCount _
                                 Where p.FromCountryId = Integer.Parse(Session("YourCountryId")) _
                                    Select p

I'm just wondering if the fact that you are selecting specific fields is preventing you getting back enitites...?

Matt

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 04-Jun-2009 22:27:45   

Also check the documentation about ILLBLGenProQuery which details how to return a native entity collection from a LINQ query.

Matt

Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 06-Jun-2009 04:26:23   

This is the error i am receiving...

Unable to cast object of type 'VB$AnonymousType_0`3[System.Int32,System.String,System.Int32]' to type 'SmallBiz.YouGoingMyWay.dal.EntityClasses.VwTripDomesticCountEntity'.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Jun-2009 08:45:08   

Hi Anthony, please do this:

GridViewDomestic.DataSource = qDomestic.ToList();

Also, when using LINQ, please make sure you are using the latest runtime libraries, and follow these instructions: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=12769

David Elizondo | LLBLGen Support Team
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 06-Jun-2009 12:48:15   

thanks but how do i retrieve a value of a property at ???? ie I have property called Country but how do i retrive it

Protected Sub GridViewDomestic_RowDataBound(....................) Handles GridViewDomestic.RowDataBound
    Dim sRowHTML As String 
    If e.Row.RowType = DataControlRowType.DataRow Then

        e.Row.Cells(1).Text = "<a href="""">" &  ???? & "</a>"
    End If

End Sub
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Jun-2009 23:02:57   

This is an ASP.Net question. You can bind the properties in the way you do that with other DataSource sources.

Here is an example and this is a helpful link.

David Elizondo | LLBLGen Support Team