Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i use three tier architecture with linq:

i use one store procedure like this:

USE [BrandMutual]
GO
/****** Object:  StoredProcedure [dbo].[SP_Product_Get]    Script Date: 03/02/2013 14:46:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_Product_Get]
(
			 @id int=0,
                   @Action nvarchar(MAX)='',
                  @category nvarchar(50)=''
		
)
As
SET NOCOUNT ON
if(@id=0)
Begin
         Select * From tbl_admin_product_add
		
                     
End


if(@id>0 AND  @Action='S1' )

Begin
 Select * From tbl_admin_product_add
 where 
  id= @id
  
  E
if(@id>0 AND  @Action='S2' )
Begin
 Select * From tbl_admin_product_add
 where 
  category = @category 
  
  End


SET NOCOUNT OFF





And a function At BLL Like This:
LinqConnectionDataContext li = new LinqConnectionDataContext();

SQL
public IEnumerable<tbl_admin_product_add> GetProductDetails()
   {
 int id=0;

       var v = li.SP_Product_Get(id);

       return (from c in v
               select new tbl_admin_product_add()
               {
                   id = c.id,
                   category = c.category,

               }
              ).ToList();


   }


so it generate Error That: No overload for method 'SP_Product_Get' takes 1 arguments

And If I pass Three arguments then it work well,



so i want that, according to my condition, i pass whatever parameters, the store procedure aromatically take other arguments NULL and Not Generate errorr
Posted
Updated 2-Mar-13 2:19am
v4

1 solution

Hi,

LINQ To SQL, will always convert the stored procedure definition into a Named Parameter method signature. So, you must pass the default values.

Anyway, you can avoid this. you just have change the Named parameter signature to optional method signature. when you drag and drop the stored procedure, the framework will generate the method signature in the designer.cs file of the context. so, just go there and assign the default values for the optional parameters. refer Named and Optional Arguments (C# Programming Guide)[^] for more.

Normally, it is not advisable to modify the system generated code. so, better you can write your own custom context class. refer below articles on how to generate custom context class, through which you solve your problem.
Stored Procedure Optional Parameters using LINQ to SQL[^]
or
Stored Procedure Optional Parameters using LINQ to SQL[^]

hope it helps.
 
Share this answer
 

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