0

我需要这方面的帮助;看起来很简单,但我就是想不通。

我的代码:

    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();
            }
        }
    }

我不断收到一个例外:

过程或函数“GetUserAssociatedModules”需要参数“@userEmail”,但未提供该参数。

我戴上了手表UserEmail;它按照应有的方式从 UI 向下传递值,但 datareader 保持为空...

我检查了我的 SQL;它执行正常。

我进行了双重和三重检查,以确保代码中调用的存储过程名称与数据库中的过程匹配,并且代码中的参数名称与过程中的参数名称匹配......

步骤...

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
4

1 回答 1

6

您缺少打开连接:

conn.Open();

或者不指定这一行:

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;
}
于 2013-02-05T03:10:23.597 回答