I am doing a test to see if the connection string user ID is a sys Admin.
But my code keeps breaking when i try return the value of the bit?
To do this i use the following stored proc:
ALTER PROCEDURE [dbo].[CheckIfAdmin]
@SQLLoginName nvarchar(50),
@IsAdmin bit OUTPUT
As
SET @IsAdmin = IS_SRVROLEMEMBER('sysadmin', @SQLLoginName)
Print @IsAdmin
Select @IsAdmin
And to retrieve the value i use the following C# code:
public bool CheckIfIsAdmin(string procedure, SqlParameter[] parameters)
{
try
{
SqlCommand cmd1 = new SqlCommand(procedure, con);
cmd1.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter p in parameters)
{
cmd1.Parameters.Add(p);
}
SqlParameter IsAdmin = new SqlParameter("@IsAdmin", SqlDbType.Bit);
IsAdmin.Direction = ParameterDirection.Output;
cmd1.Parameters.Add(IsAdmin);
con.Open();
con.Close();
MessageBox.Show("Is a admin = " + IsAdmin.Value.ToString()); // this should display 1 or 0
return ture
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
return false;
}
}
The Error message:
NullReference Exception was caught.
Object reference not set to an instance of an object.
You’ve missed the
ExecuteXXXXX() method.