Linq Stored Procedure type error

Posted on 2008-11-13
Last Modified: 2013-11-11
I have searched the web for a solution to ym problem but have yet to find someone the same issue...I am using SQL Stored procedures with LINQ2SQL and 3.5 framework. So far I have worked out the correct datatypes and return types for my calls but one call is really annoying me...and the error being returned is as follows -

System.InvalidCastException: Unable to cast object of type 'SingleResult`1[SelectTemplateAreasResult]' to type 'System.Web.UI.WebControls.DataList'.

Now the stored procedure I am calling can return both single and multiple results. I am thinking I need to use "List (of Templatearea)" but not sure how I use this new type and how to pipe it.

I have included the coding below, I would be grateful fi someone could shed some light on the correct method. Many Thanks, Simon


Dim sdc As New SocialiteDataContext()

Dim dlTemplateAreas As New DataList

dlTemplateAreas = sdc.SelectTemplateAreas(-1, Request.QueryString("TemplateID"))

Socialite.DBML Coding


<FunctionAttribute(Name:="dbo.SelectTemplateAreas")>  _

	Public Function SelectTemplateAreas(<Parameter(Name:="AreaID", DbType:="Int")> ByVal areaID As System.Nullable(Of Integer), <Parameter(Name:="TemplateID", DbType:="Int")> ByVal templateID As System.Nullable(Of Integer)) As ISingleResult(Of SelectTemplateAreasResult)

		Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod,MethodInfo), areaID, templateID)

		Return CType(result.ReturnValue,ISingleResult(Of SelectTemplateAreasResult))

	End Function



CREATE PROCEDURE SelectTemplateAreas

@AreaID int, @TemplateID int


SELECT * FROM TemplateAreas WHERE (@AreaID = -1 OR ID = @AreaID) AND (@TemplateID = -1 OR TemplateID = @TemplateID)


Open in new window

Question by:clearchannel
    LVL 7

    Accepted Solution

    I belive the problem your descring comes from line 6:
    dlTemplateAreas = sdc.SelectTemplateAreas(-1, Request.QueryString("TemplateID")). Where you're trying to assign the data on to the WebControl, instead on to the WebControl's DataSource.

    dlTemplateAreas.DataSource = sdc.SelectTemplateAreas(-1, Request.QueryString("TemplateID"))

    Open in new window


    Author Comment

    Hahahahaha I didn't even notice my stupid mistake!!! I feel like a fool.

    I was looking for all sorts of answers but missed that I hadn't added back in the datasource and databind methods after converting from dataset to datalist!

    Let me confirm that is the only issue and I'll close the question!
    LVL 7

    Expert Comment

    Happy to help ;)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now