Click here to Skip to main content
15,909,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any one tell me how to use output parameter in sp and retrive it in c# code.My sp is as follow.

SQL
ALTER PROCEDURE [dbo].[ssp_AgriShowdata]
@temp float,
@lpH INT OUTPUT,
@lEC INT OUTPUT,
@lTexture INT OUTPUT,
@lNitrogen INT OUTPUT,
@lPhosphorus INT OUTPUT,
@lPotassium INT OUTPUT
AS
BEGIN
Select C.c_SerialNo,C.c_FirstName,C.c_MiddleName,C.c_LastName,C.c_addr,C.c_Taluka,C.c_SurveyNo,C.c_ReportNumber,C.a_pH,C.a_EC,C.a_Texture,C.a_Nitrogen,C.a_Phosphorus,C.a_Potassium,C.s_Magnessium,C.s_Calcium,C.s_Sulphur,C.m_Zinc,C.m_Iron,C.m_Manganese,C.m_Copper,C.m_Boron,C.c_Date,@lpH=C.l_pH /*as '+@lpH+'*/ ,C.l_EC,C.l_Texture,C.l_Nitrogen,C.l_Phosphorus,C.l_Potassium,R.r_Crop,R.r_Nitrogen,R.r_Phosphorus,R.r_Potassium,R.r_DAP,R.r_Urea,R.r_PotashMOP,R.r_ZincSulphate,R.r_IronSulphate,R.r_ManganeseSulphate,R.r_CopperSulphate,R.r_Borax,R.r_GreenManure,R.r_FYMCompost,R.r_LimeGypsum,R.r_AfterEveryYears FROM Cultivator_tbl C LEFT OUTER JOIN Recommendation_tbl  R on C.Cid=@temp AND R.Did=@temp ORDER BY C.c_FirstName ASC,R.Rid ASC ;
END

in the sp when i write @lpH=C.l_pH it shows me some error for C.c_SerialNo
Posted
Updated 11-Aug-13 21:21pm
v2

 
Share this answer
 
Comments
Rakhil Naik 12-Aug-13 3:28am    
i hv 2 return @lpH,@lEC,@lTexture,@lNitrogen,@lPhosphorus,@lPotassium
can u tell me how to assign selected value from select query into those OUTPUT variable declared above?
Maciej Los 12-Aug-13 3:35am    
Have you read my answer and articles? No! Please, read it and then ask me again.
Your stored procedure has errors. I dont think assigment and returning result set at the same time is allowed in SQL Server. Change your SP in following order and debug.

SQL
select @lpH=C.l_pH /*, rest of the parameters assigment */
FROM Cultivator_tbl C LEFT OUTER JOIN Recommendation_tbl  R on C.Cid=@temp AND R.Did=@temp ORDER BY C.c_FirstName ASC,R.Rid ASC ;
 
Share this answer
 
Comments
Rakhil Naik 12-Aug-13 5:33am    
thanks
Can u tell me how to get those value in c# code?
SqlParameter lpH = new SqlParameter("@lpH", SqlDbType.Int);
lpH.Direction = ParameterDirection.Output;
cmd.Parameters.Add(lpH);
In the above code when i execute i get @lph as a value in lph.Can u help me out with this?
_Asif_ 12-Aug-13 6:21am    
After execution of the SP do this

int returnValue = (int)(Command1.Parameters["lpH"].Value);
if you're using linq to sql classes, you can store the result in a temporary variable

C#
...

try
{
   var res = {placedatacontextvariablehere}.ssp_AgriShowdata({sp parameters});

   //consume the result set

   //if it's a list
   foreach(var r in res)
   { 
      // to-do
      ... r. ...
   }
   //or any other looping mechanism you prefer
   
   

 } catch {}
 
Share this answer
 
v3

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