Click here to Skip to main content
15,898,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

C#
public string GetUserRole(ManageUserRole modelManageUserRole)
        {

            object status = new object();

            DataBaseHelper context = new DataBaseHelper();
            SqlParameter[] prm;
            prm = new SqlParameter[] {
                new SqlParameter ("@UserName" , SqlDbType .VarChar),
                new SqlParameter ("@msg" , SqlDbType .VarChar ,100)
            };
            prm[0].Value = modelManageUserRole.userName;

            prm[1].Direction = ParameterDirection.Output;


           status = context.GetReaderWithPrm("usp_GetUserRole", prm);

            string outPutValues = prm[1].Value.ToString();
            return outPutValues;
Posted
Comments
OriginalGriff 10-Feb-14 7:47am    
Which line gives the error?
Member 10546331 10-Feb-14 7:52am    
string outPutValues = prm[1].Value.ToString();
this line is giving the error
Maarten Kools 10-Feb-14 7:53am    
Perhaps the status returned by the stored procedure indicates an error occurred. I would assume the Value property remains null, hence the error.
Member 10546331 10-Feb-14 7:53am    
string outPutValues = prm[1].Value.ToString();
this line is giving the error
Member 10546331 10-Feb-14 7:53am    
string outPutValues = prm[1].Value.ToString();

string outPutValues = prm[1].Value.ToString();

this line is giving the error

Well...I'm not surprised...
C#
prm = new SqlParameter[] {
    new SqlParameter ("@UserName" , SqlDbType .VarChar),
    new SqlParameter ("@msg" , SqlDbType .VarChar ,100)
};
prm[0].Value = modelManageUserRole.userName;
prm[1].Direction = ParameterDirection.Output;
status = context.GetReaderWithPrm("usp_GetUserRole", prm);
string outPutValues = prm[1].Value.ToString();
You don't assign any value to the Value property of prm[1], so when you try to call ToString, it's working from a null value, and you get the exception.
If your context.GetReaderWithPrm method is calling a stored procedure which returns a value via the parameter, then you need to set it as an Output parameter in order to get values back.

Otherwise, check your method and see what it does with the parameters and if it modifies them in any way.
 
Share this answer
 
Comments
Member 10546331 10-Feb-14 8:09am    
ALTER PROCEDURE [dbo].[usp_GetUserRole]
@UserName varchar(100),@msg varchar(100) output

AS
declare @countusername int
BEGIN
begin tran
select @countusername = COUNT (*) from tm_User where UserName = @UserName
if(@countusername = 0)
begin
select @msg = 'User does not exist'
end
else
select UserRole from tm_User where UserName = @UserName

COMMIT TRAN
END this is my stored procedure
OriginalGriff 10-Feb-14 8:14am    
And it uses an OUTPUT parameter.
But you don't tell the SqlParameter that, so it doesn't do anything with it!
Try setting the SqlParameter.Direction property to ParameterDirection.Output
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.direction(v=vs.110).aspx
try this

string outPutValues = prm[1].Value.ToString();

C#
string outPutValues = Convert.ToString(prm[1].Value);
       // or
       string outPutValues = prm[1].Value + "";
 
Share this answer
 
Comments
Member 10546331 10-Feb-14 8:36am    
thanks it worked...bt i want userrole to be returned ...my stored proc is
ALTER PROCEDURE [dbo].[usp_GetUserRole]
@UserName varchar(100),@msg varchar(100) output

AS
declare @countusername int
BEGIN
begin tran
select @countusername = COUNT (*) from tm_User where UserName = @UserName
if(@countusername = 0)
begin
select @msg = 'User does not exist'
end
else
select UserRole from tm_User where UserName = @UserName

COMMIT TRAN
END this is my stored procedure
Karthik_Mahalingam 10-Feb-14 8:42am    
select @msg = 'User does not exist'
end
else
select UserRole from tm_User where UserName = @UserName
select @msg = UserRole from tm_User where UserName = @UserName
Member 10546331 10-Feb-14 10:35am    
still it is not returning any role...
Karthik_Mahalingam 10-Feb-14 13:45pm    
can u post full sql code.
Member 10546331 11-Feb-14 0:26am    
ALTER PROCEDURE [dbo].[usp_GetUserRole]
@UserName varchar(100),@msg varchar(100) output

AS
declare @countusername int
BEGIN
begin tran
select @countusername = COUNT (*) from tm_User where UserName = @UserName
if(@countusername = 0)
begin
select @msg = 'User does not exist'
end
else
select UserRole from tm_User where UserName = @UserName

COMMIT TRAN
full sql code

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