i want to pass integer value to the SqlParameter which is executing an SqlDataReader, but when i pass integer value it says, the Parameter is not supplied, below is my code:
Common SqlDataReader Function
public static SqlDataReader ExecuteReader(string procedure, SqlParameter[] parameters, CommandType commandType)
{
SqlDataReader reader = null;
SqlConnection connection = new SqlConnection(connectionString);
using (SqlCommand command = new SqlCommand(procedure, connection))
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
if (parameters != null)
{
if (commandType == CommandType.StoredProcedure)
command.Parameters.AddRange(parameters);
}
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
}
return reader;
}
Calling code:
SqlDataReader obj = SqlHelper.ExecuteReader("sp_TheWireUser", new SqlParameter[] { new SqlParameter("@USERID", Convert.ToInt32(1)), new SqlParameter("@ROLEID", Convert.ToInt32(6)) }, System.Data.CommandType.StoredProcedure);
Response.Write(obj.HasRows);
i tried entering 1 as a parameter also "1" but all does not work
ALTER PROCEDURE [dbo].[sp_TheWireUser]
@USERID INT,
@RoleID INT
AS
BEGIN
SELECT USR.USERID FROM Users USR
INNER JOIN UserRoles UR
ON USR.UserID = UR.UserID
INNER JOIN Roles R
ON UR.RoleID = R.RoleID
WHERE R.RoleID = @RoleID
AND USR.UserID = @USERID
END
Why are you converting
1to Integer by calling Convert.ToInt32() .1is already integer. So you do not need to do that.This code should not throw any compile time errors. If you are getting a runtime exception, The possible reason is your Stored Procs paramter’s data type is different than what you are passing here. It should be
Integeras per your code.Note : Hope you are closing the
DataReaderafter use.EDIT : You have to tell the
CommandType