Abbas
Abbas

Reputation: 5044

Passing Integer values to SqlParameter in .net

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

Upvotes: 0

Views: 4584

Answers (2)

Shyju
Shyju

Reputation: 218812

Why are you converting 1 to Integer by calling Convert.ToInt32() .1 is already integer. So you do not need to do that.

SqlDataReader obj = ExecuteReader("sp_TheWireUser", 
                new SqlParameter[] { 
                    new SqlParameter("@USERID", 1),
                    new SqlParameter("@ROLEID", 6)
                },
                System.Data.CommandType.StoredProcedure);

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 Integer as per your code.

Note : Hope you are closing the DataReader after use.

EDIT : You have to tell the CommandType

 using (SqlCommand command = new SqlCommand(procedure, connection))
 {
    command.CommandType = commandType; //passed from your method parameter
    //your remaining code
 }

Upvotes: 1

Mark
Mark

Reputation: 1537

Take a look at a similar question I asked in the past. Cleaning Up Resources via Using Statement & Dispose for DataTable, SqlConnection, SqlCommand, & SqlDataAdapter

It will show you how to clean up your data and also how to add a parameter. Also, wait until the last minute to Open your connection, i.e., add the parameter first.

Upvotes: 0

Related Questions