Hi i'm creating my Data Access Layer and I want to return my object but not all the fields so this is the code i'm using

Public Function BasicSearchProposals(ByVal searchvalue As String) As IEnumerable(Of DCProposal)
            Dim dc As New PPDataContext()

            Dim proposals = From p In dc.DCProposals _
                            Where p.RefNum = searchvalue Or _
                            p.FormattedrefNum = searchvalue Or _
                            p.UniquePropertyReference = searchvalue Or _
                            p.ApplicantName = searchvalue Or _
                            p.AgentName = searchvalue Or _
                            p.ApplicantPostCode = searchvalue Or _
                            p.SitePostCode = searchvalue Or _
                            p.ahDescription.Contains(searchvalue) Or _
                            p.AgentPostCode.Equals(searchvalue) _
                            Select New With { _
                            .RefNum = p.RefNum, _
                            .ApplicantName = p.ApplicantName, _
                            .UniquePropRef = p.UniquePropertyReference}

            Return proposals
        End Function

I then call this method in the Business Logic Layer

Public Shared Function BasicProposalSearch(ByVal searchVal As String) As IEnumerable(Of DCProposal)
            Dim dal As New ProposalDAL()
            Return dal.BasicSearchProposals(searchVal)
        End Function

lastly I try to call in in my user interface and bind it to a DataGridView

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        dgvProposals.DataSource = PP_BLL.BLL.ProposalBLL.BasicProposalSearch(txtSearch.Text)
        
    End Sub

but I keep getting this error
Unable to cast object of type 'System.Data.Linq.DataQuery`1[VB$AnonymousType_0`3[System.String,System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[PP_DAL.DCProposal]'.

please help me to correct it

Recommended Answers

All 2 Replies

Hi i'm creating my Data Access Layer and I want to return my object but not all the fields so this is the code i'm using

Public Function BasicSearchProposals(ByVal searchvalue As String) As IEnumerable(Of DCProposal)
            Dim dc As New PPDataContext()

            Dim proposals = From p In dc.DCProposals _
                            Where p.RefNum = searchvalue Or _
                            p.FormattedrefNum = searchvalue Or _
                            p.UniquePropertyReference = searchvalue Or _
                            p.ApplicantName = searchvalue Or _
                            p.AgentName = searchvalue Or _
                            p.ApplicantPostCode = searchvalue Or _
                            p.SitePostCode = searchvalue Or _
                            p.ahDescription.Contains(searchvalue) Or _
                            p.AgentPostCode.Equals(searchvalue) _
                            Select New With { _
                            .RefNum = p.RefNum, _
                            .ApplicantName = p.ApplicantName, _
                            .UniquePropRef = p.UniquePropertyReference}

            Return proposals
        End Function

I then call this method in the Business Logic Layer

Public Shared Function BasicProposalSearch(ByVal searchVal As String) As IEnumerable(Of DCProposal)
            Dim dal As New ProposalDAL()
            Return dal.BasicSearchProposals(searchVal)
        End Function

lastly I try to call in in my user interface and bind it to a DataGridView

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        dgvProposals.DataSource = PP_BLL.BLL.ProposalBLL.BasicProposalSearch(txtSearch.Text)
        
    End Sub

but I keep getting this error
Unable to cast object of type 'System.Data.Linq.DataQuery`1[VB$AnonymousType_0`3[System.String,System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable`1[PP_DAL.DCProposal]'.

please help me to correct it

Ok figured it out

in the DAL

Public Function BasicSearchProposals(ByVal searchvalue As String) As IQueryable
            Dim dc As New PPDataContext()
            Dim proposals = From p In dc.DCProposals _
                            Where p.RefNum = searchvalue Or _
                            p.FormattedrefNum = searchvalue Or _
                            p.UniquePropertyReference = searchvalue Or _
                            p.ApplicantName = searchvalue Or _
                            p.AgentName = searchvalue Or _
                            p.ApplicantPostCode = searchvalue Or _
                            p.SitePostCode = searchvalue Or _
                            p.ahDescription.Contains(searchvalue) Or _
                            p.AgentPostCode.Equals(searchvalue) _
                            Select New With { _
                            .RefNum = p.RefNum, _
                            .ApplicantName = p.ApplicantName, _
                            .UniquePropRef = p.UniquePropertyReference}

            Return proposals
        End Function

in the BLL

Public Shared Function BasicProposalSearch(ByVal searchVal As String) As IQueryable
            Dim dal As New ProposalDAL()
            Return dal.BasicSearchProposals(searchVal)
        End Function

and lastly in the UI

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        dgvProposals.DataSource = PP_BLL.BLL.ProposalBLL.BasicProposalSearch(txtSearch.Text)
        
    End Sub

No problem. Please mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.