Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NETLINQ
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
Edited 26-Feb-13 2:37am
v5
Comments
Karthik Harve at 26-Feb-13 5:26am
   
where have you used LINQ here ?
Arun kumar Gauttam at 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 at 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 at 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 at 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 at 26-Feb-13 7:46am
   
what was the error message?
Arun kumar Gauttam at 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 at 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 at 26-Feb-13 23:53pm
   
wHAT Is Class1() In your Case
Arun kumar Gauttam at 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 442
1 sanket saxena 355
2 Abhinav S 268
3 OriginalGriff 250
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid