Click here to Skip to main content
14,774,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 26-Feb-13 3: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]'.
bbirajdar 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

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();
   
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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900