Click here to Skip to main content
13,150,471 members (37,686 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I Am Working 3 tier Architecture with linq and store procedure:

I insert and update And Delete Record Successfully,

But When Secting Record From table Then there is an error releted to type casting:

I have used following code at BLL:

public List<tbl_admin_category> GetCategoryDetails(int id)
   {
 
       var v = li.SP_Category_Get(id);
 
       return ((List<tbl_admin_category>)(v));
 

   }


And At aspx.cs page:

var v1 = obj.GetCategoryDetails(0);
      gd_cat.DataSource = v1;
      gd_cat.DataBind();



And Procedure Is:

ALTER PROCEDURE [dbo].[SP_Category_Get]
(
             @id int=0
 
)
As
SET NOCOUNT ON
if(@id=0)
Begin
         Select * From tbl_admin_category
 

End
 
else
Begin
 Select * From tbl_admin_category
 where
  id= @id
  End
SET NOCOUNT OFF



ERROR Msg IS:
Unable to cast object of type 'SingleResult`1[SP_Category_GetResult]' to type 'System.Collections.Generic.List`1[tbl_admin_category]'.





And If I not Use Store Procedure:
And Use Simple Method In BLL Then It Work Fine:

Like:
public IEnumerable<tbl_admin_category> GetCategoryDetails(int id)
   {
 

           var users = from user in li.tbl_admin_categories
                       select user;
           return users.ToList();
 
   }
Posted 25-Feb-13 23:16pm
Updated 26-Feb-13 2:37am
v5
Comments
Karthik Harve 26-Feb-13 5:26am
   
where have you used LINQ here ?
Arun kumar Gauttam 26-Feb-13 5:43am
   
i used li as linq class connection obeject:
At BLL
LinqConnectionDataContext li = new LinqConnectionDataContext();


i used LinqConnectionDataContext As DAL:
Arun kumar Gauttam 26-Feb-13 5:49am
   
i use This Article For Linq With 3 tier:
http://www.c-sharpcorner.com/uploadfile/a72401/linq-with-3-layer-architecture-insert-data-into-database/
Matej Hlatky 26-Feb-13 7:39am
   
What is the SP_Category_Get method originally returning? I think it would be primitive IEnumerable(Of tbl_admin_category) rather than List(Of tbl_admin_category).
Arun kumar Gauttam 26-Feb-13 8:34am
   
I Try All Type Of Conversion And Try All Type Of Return (Like: List,IEnumerable,ISingleResult) But Error is Always Generate:


And If I not Use Store Procedure And Use This Method At BLL then No error Generated, And Code Work Well:

public IEnumerable<tbl_admin_category> GetCategoryDetails(int id)
{


var users = from user in li.tbl_admin_categories
select user;
return users.ToList();

}
Tejas Vaishnav 26-Feb-13 7:46am
   
what was the error message?
Arun kumar Gauttam 26-Feb-13 8:29am
   
Error Msg Is:

Unable to cast object of type 'SingleResult`1[SP_Category_GetResult]' to type 'System.Collections.Generic.List`1[tbl_admin_category]'.
aspnet_regiis -i 26-Feb-13 9:33am
   
You are gettings a single object here
var v = li.SP_Category_Get(id);

And you are trying to cast it to List here
((List<tbl_admin_category>)(v));

Then offcourse you will get the error...

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use can get you data like this.

var v = li.SP_Category_Get(id);
           return (from c in v
                   select new Class1() {
                      Property1 = c.value1,
                      Property2 =  c.value2
                   }
                  ).ToList();
  Permalink  
Comments
Arun kumar Gauttam 26-Feb-13 23:53pm
   
wHAT Is Class1() In your Case
Arun kumar Gauttam 27-Feb-13 0:07am
   
I use It Like This And it Work Well:
thanks for it
var v = li.SP_Category_Get(id);

return (from c in v
select new tbl_admin_category()
{
id=c.id,
category = c.category,
category_img = c.category_img
}
).ToList();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100