简体   繁体   中英

Parameter not passing into SQL Server stored procedure

I need help with this; seems straightforward but I just can't figure it out.

My code:

    public DataTable GetUserAssociatedModules(string UserEmail)
    {
        // retrieve module titles from Module table to be passed to ddlModules in Student.Master
        try
        {
            SqlCommand getModules = new SqlCommand("GetUserAssociatedModules", con);
            getModules.Parameters.AddWithValue("@userEmail", UserEmail);

            SqlDataReader dr;
            dr = getModules.ExecuteReader();

            //DataTable dt = new DataTable();
            dt.Columns.Add("moduleTitle");
            dt.Load(dr);
            return dt;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("Exception: " + ex.Message + ", CourseDAO.GetModules()");
            return null;
        }
        finally
        {
            // close database connection
            if (con != null)
            {
                con.Close();
            }
        }
    }

I keep getting an exception:

Procedure or function 'GetUserAssociatedModules' expects parameter '@userEmail', which was not supplied.

I put aa watch on UserEmail ; it's passing the value down from the UI as it should but the datareader remains null...

I've checked my SQL; it executes OK.

I've double and triple checked to ensure stored procedure name called in code matches the procedure in the database and that the parameter name in the code matches the one in the procedure...

The procedure...

ALTER PROCEDURE [dbo].[GetUserAssociatedModules]
    @userEmail nvarchar(MAX)
AS
BEGIN
    SELECT 
        [dbo].[Module].[moduleId],[moduleTitle] 
    FROM 
        [dbo].[Module] 
    INNER JOIN 
        [dbo].[ModuleUser] ON [dbo].[Module].[moduleId] = [dbo].[ModuleUser].[moduleId]
    WHERE 
        [dbo].[ModuleUser].[userId] = (SELECT [userId]
                                       FROM [dbo].[User]
                                       WHERE [eMail] = @userEmail)
    ORDER BY 
        [moduleTitle] ASC
END

You are missing either opening the connection:

conn.Open();

Or not specifying this line:

sqlCommand.CommandType = CommandType.StoredProcedure;

public DataTable GetUserAssociatedModules(string userEmail)
{
var dt = new DataTable();
string connString = AppConfigurationManager.GetAppSetting("ConnectionString",IsTest,IsQA, IsProd);
using (var conn = new SqlConnection(connString))
{
    conn.Open();
    using (var sqlCommand = new SqlCommand("GetUserAssociatedModules", conn))
    {
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.Parameters.AddWithValue("@userEmail", userEmail);

        SqlDataReader dr = null;
        DataSet ds = new DataSet();
        try
        {
            dr = sqlCommand.ExecuteReader();
            dt = new DataTable("DatatTable_GetUserAssociatedModules");
            ds.Tables.Add(dt);
            ds.Load(dr, LoadOption.OverwriteChanges, dt);
        }
        catch (SqlException ex)
        {
            LogExceptions(ex, new List<Param> { new Param { Name = "userEmail", Value = userEmail} });
         }
        catch (Exception ex)
        {
            LogExceptions(ex, new List<Param> { new Param { Name = "userEmail", Value = userEmail} });
        }
        finally
        {

        }
    }
}
return dt;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM