2015-04-03 89 views
-1

我想在我的文本框中顯示數據,並且在我的DAL中遇到錯誤。我有一個存儲過程,但我無法讓我的數據返回返回值。我想用我的BLL返回一個DataTable。請你幫忙嗎?ExecuteReader:連接錯誤消息

DAL

public static string GetTicket(collection b) 
     { 
      try 
      { 

       string returnValue = string.Empty; 
       DB = Connect(); 
       DBCommand = connection.Procedure("getTicket"); 
       DB.AddInParameter(DBCommand, "@SupportRef", DbType.String, b.SupportRef1); 

       var myReader = DBCommand.ExecuteReader(); 
       while (myReader.Read()) 
       { 
        returnValue = myReader.GetString(0); 
       } 

       return returnValue; 
      } 

      catch (Exception ex) 
      { 
       throw ex; 
      } 

     } 

我收到以下錯誤:

ExecuteReader: Connection property has not been initialized. 

我使用的連接類,看起來像這樣:

public class connection 
{ 
    const string StrConnection = "TicketHelperConnectionString"; 
    internal static Database DB; 
    public static DbCommand DBCommand; 
    public static Database Connect() 
    { 

     try 
     { 
      DB = DatabaseFactory.CreateDatabase(StrConnection); 
      return DB; 
     } 
     catch (Exception ex) 
     { 
      throw (ex); 
     } 
    } 
    public static DbCommand Procedure(string procedure) 
    { 

     try 
     { 
      DBCommand = DB.GetStoredProcCommand(procedure); 
      return DBCommand; 
     } 
     catch (Exception ex) 
     { 
      throw (ex);    
     } 
    } 
} 

回答

0

看起來像連接屬性DBCommand沒有設置,在你之前從Procedure方法轉你的命令你必須設置其連接屬性:

DBCommand = DB.GetStoredProcCommand(procedure); 

// DBCommand.Connection = ... 

return DBCommand;