2013-02-05 32 views
0

我需要幫助;似乎很直接,但我無法弄清楚。參數未傳入SQL Server存儲過程

我的代碼:

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;它的價值從用戶界面向下傳遞,因爲它應該但datareader保持空...

我檢查了我的SQL;它執行OK。

我有雙重和三重檢查,以確保調用代碼存儲過程的名稱的程序在數據庫中,並在代碼中的參數名稱相匹配的在程序匹配...

的過程.. 。

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 
+0

發佈您的SP! –

回答

5

你缺少要麼打開連接:

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; 
} 
+0

我遺漏了CommandType ...感謝百萬.. –

+0

你遺漏了比那個用戶多很多1748443 – MethodMan