Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET ADO.NET
hi i have wrote a stored procedure which returns one output value i have to read that value in my code which was giving me an error please correct
create proc Authenticate(@Pname varchar(50),@categoryname varchar(55))
as
begin
declare @productcount int
declare @categorycount int
set @productcount=(select count(*) from Product where Pname=@Pname )
set @categorycount=(select count(*)from Category where CategoryName=@categoryname)
if(@productcount!=0  @categorycount!=0)
return 1
else
return 0
end
C# Code Below
 
     
          SqlConnection con = new SqlConnection(Constr);
          SqlCommand cmd = new SqlCommand("Authenticate",con);
          cmd.Parameters.AddWithValue("@Pname", productname);
          cmd.Parameters.AddWithValue("@CategoryName", categoryname);
          con.Open();
          int ctr = (int)cmd.ExecuteScalar();
          con.Close();
          return ctr;
Posted 12-Apr-13 1:17am
Edited 12-Apr-13 3:13am
v6
Comments
ThePhantomUpvoter at 12-Apr-13 9:12am
   
Would really be helpful if you told us what the error was instead of trying to make us guess. You need to set the command type to be CommandType.StoredProcedure.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you read the code example of SqlParameterCollection.AddWithValue here[^] you'll see that you're missing the @ prefix of the parameter's name.
 
— Manfred
  Permalink  
Comments
surendranew at 12-Apr-13 8:14am
   
i have updated the question manfred please see one more time
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
If you want to display the error message using StoredProcedure then you must create one output parameter and then call that parameter in your code, please refer below link to get output parameter from StoredProcedure to ADO.net
 

http://www.c-sharpcorner.com/UploadFile/rohatash/get-out-parameter-from-a-stored-procedure-in-Asp-Net/[^]
 
http://www.aspdotnet-suresh.com/2012/07/get-output-parameter-from-stored.html[^]
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 DamithSL 165
4 Tomas Takac 144
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2014
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