Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C# code is

public void GetUserLoginName(string Username)
{

SqlConnection sqlcon;
SqlCommand sqlcmd;
try
{
sqlconnstring = ConfigurationManager.ConnectionStrings["CONERP"].ConnectionString;
sqlcon = new SqlConnection(sqlconnstring);
sqlcon.Open();
sqlcmd = new SqlCommand("GetUserLoginName", sqlcon);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = Username;
SqlParameter rolename = sqlcmd.Parameters.Add("@role", SqlDbType.VarChar);
rolename.Direction = ParameterDirection.Output;
Session["Loginusername"] = Convert.ToString(rolename.Value) ;
sqlcmd.Dispose();
sqlcon.Close();

}
catch (SqlException sqlerr)
{

}
}
and sql stored procedure is

USE [COERP]
GO
/****** Object: StoredProcedure [dbo].[GetUserLoginName] Script Date: 03/07/2014 13:34:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[GetUserLoginName](
@role varchar(max) out,
@Username varchar(max)
)

as
begin
set @role =(select Name from Newuser where Username=@Username)
end


the output of this code is '@role' not the value inside @role !!
Posted
Updated 6-Mar-14 21:17pm
v3
Comments
Raajkumar.b 7-Mar-14 1:54am    
check this link http://www.sqlteam.com/article/stored-procedures-returning-data
Replace these lines with below code

sqlcmd.Parameters.Add("@role", SqlDbType.VarChar).Direction = ParameterDirection.Output;
string name = Convert.ToString(sqlcmd.ExecuteReader().ToString());


SqlParameter rolename= cmd.Parameters.Add("@role", SqlDbType.varchar);
rolename.Direction = ParameterDirection.Output;
you can get your result in rolename.
Sidharth R 7-Mar-14 2:11am    
my store procedure is

USE [COERP]
GO
/****** Object: StoredProcedure [dbo].[GetUserLoginName] Script Date: 03/07/2014 12:39:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[GetUserLoginName](
@role varchar(max) output ,
@Username varchar(max)
)

as
begin
select @role = ( Select Name from Newuser where Username=@Username)

end

is there any error in this ??
Raajkumar.b 7-Mar-14 2:22am    
select @role = ( Name from Newuser where Username=@Username)
Sidharth R 7-Mar-14 3:14am    
but im getting the out put as '@role' ?? not the value inside that ..!!
Raajkumar.b 7-Mar-14 3:28am    
what are fields of Newuser Table

Two things you need to change:

1. "Out" should be added in your Stored Procedure to @role declaration which is missing.
2. Instead of returning just Select or SET the value of @role (if you are assigning a value to it)
 
Share this answer
 
Have a look here: CREATE PROCEDURE[^] and go to C. Using OUTPUT parameters section ;)
And here[^] you'll find information about how to execture SP ;)
 
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