• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

Linq Stored Procedure type error

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 asp.net 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
ASP.NET Code
=============
 
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
 
 
SP
==
 
CREATE PROCEDURE SelectTemplateAreas
@AreaID int, @TemplateID int
AS
SELECT * FROM TemplateAreas WHERE (@AreaID = -1 OR ID = @AreaID) AND (@TemplateID = -1 OR TemplateID = @TemplateID)
GO

Open in new window

0
clearchannel
Asked:
clearchannel
  • 2
1 Solution
 
moseackCommented:
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"))
dlTemplateAreas.DataBind()

Open in new window

0
 
clearchannelAuthor Commented:
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!
0
 
moseackCommented:
Happy to help ;)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now