2017-05-25 69 views
1

我正在嘗試遍歷SQL查詢並將對象參數設置爲特定值。似乎正常工作的唯一值是workpack.JobCardIDs,因爲我可以實現一個foreach循環來顯示結果。例如,如果我嘗試將Label的Text屬性設置爲workpack.WorkPackTitle,則即使數據庫值是針對每行的內容,也會顯示爲空白。從SQL查詢設置對象值

我對OOP相當陌生,所以不能完全確定是否有某些我缺少的東西是根本。

public class WorkPack 
    { 
     public int ID { get; set; } 
     public string WorkPackNumber { get; set; } 
     public string WorkPackTitle { get; set; } 
     public string WorkPackDescription { get; set; } 
     public Boolean IFC { get; set; } 
     public string SPA { get; set; } 
     public string Writer { get; set; } 
     public string Organization { get; set; } 
     public List<int> JobCardIDs { get; set; } 
     public int JobCard { get; set; } 
    } 

    public static WorkPack PopulateWorkPackObject(WorkPack workpack, int workPackID) 
    { 
     string ConnectionString = ConfigurationManager.ConnectionStrings["vmdatamanagerConnectionString"].ConnectionString; 
     string sqlCall = "I HAVE REMOVED CALL BUT VERIFIED IT FUNCTIONS (SELECT columns FROM workpackdatabase where workpackname = x"; 

     using (SqlConnection con = new SqlConnection(ConnectionString)) 
     { 
      using (SqlCommand cmd = new SqlCommand(sqlCall, con)) 
      { 
       cmd.Connection.Open(); 
       using (SqlDataReader reader = cmd.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         if (reader.IsDBNull(reader.GetOrdinal("PARAM1")) == false) 
          workpack.WorkPackNumber = (reader.GetString(reader.GetOrdinal("PARAM1"))); 

         if (reader.IsDBNull(reader.GetOrdinal("PARAM2")) == false) 
          workpack.WorkPackTitle = reader.GetString(reader.GetOrdinal("PARAM2")); 

         if (reader.IsDBNull(reader.GetOrdinal("PARAM3")) == false) 
          workpack.WorkPackDescription = reader.GetString(reader.GetOrdinal("PARAM3")); 

         if (reader.IsDBNull(reader.GetOrdinal("PARAM4")) == false) 
          workpack.IFC = reader.GetBoolean(reader.GetOrdinal("PARAM4")); 

         if (reader.IsDBNull(reader.GetOrdinal("PARAM5")) == false) 
          workpack.SPA = reader.GetString(reader.GetOrdinal("PARAM5")); 

         if (reader.IsDBNull(reader.GetOrdinal("PARAM6")) == false) 
          workpack.Writer = reader.GetString(reader.GetOrdinal("PARAM6")); 

         if (reader.IsDBNull(reader.GetOrdinal("PARAM7")) == false) 
          workpack.Organization = reader.GetString(reader.GetOrdinal("PARAM7")); 

         if (reader.IsDBNull(reader.GetOrdinal("PARAM8")) == false) 
          jobCardIDs.Add(reader.GetInt32(reader.GetOrdinal("PARAM8"))); 

        } 
        workpack.JobCardIDs = jobCardIDs; 
        return workpack; 
       } 
      } 
     } 
    } 
+0

如果你在while(reader.Read())行放置斷點,它是否進入循環內部,還是完全跳過它? – mjw

+0

它進入循環內部,它遍歷循環並將記錄添加到jobCardID列表中。然後當方法外我可以調用workpack.JobCardIDs,它可以被讀取。工作包對象的每個其他元素都是空白的。它是一個奇怪的。 –

回答

0

看起來像你永遠不會創建你的jobCardID列表的本地實例。你只需要在你的ExecuteReader塊內部執行此操作。見下文。 GL

public static WorkPack PopulateWorkPackObject(WorkPack workpack, int workPackID) 
{ 
    string ConnectionString = ConfigurationManager.ConnectionStrings["vmdatamanagerConnectionString"].ConnectionString; 
    string sqlCall = "I HAVE REMOVED CALL BUT VERIFIED IT FUNCTIONS (SELECT columns FROM workpackdatabase where workpackname = x"; 

    using (SqlConnection con = new SqlConnection(ConnectionString)) 
    { 
     using (SqlCommand cmd = new SqlCommand(sqlCall, con)) 
     { 
      cmd.Connection.Open(); 
      using (SqlDataReader reader = cmd.ExecuteReader()) 
      { 
       List<int> jobCardIDs = new List<int>(); //<--***THIS IS THE LINE YOU NEED TO ADD*** 
       while (reader.Read()) 
       { 
        if (reader.IsDBNull(reader.GetOrdinal("PARAM1")) == false) 
         workpack.WorkPackNumber = (reader.GetString(reader.GetOrdinal("PARAM1"))); 

        if (reader.IsDBNull(reader.GetOrdinal("PARAM2")) == false) 
         workpack.WorkPackTitle = reader.GetString(reader.GetOrdinal("PARAM2")); 

        if (reader.IsDBNull(reader.GetOrdinal("PARAM3")) == false) 
         workpack.WorkPackDescription = reader.GetString(reader.GetOrdinal("PARAM3")); 

        if (reader.IsDBNull(reader.GetOrdinal("PARAM4")) == false) 
         workpack.IFC = reader.GetBoolean(reader.GetOrdinal("PARAM4")); 

        if (reader.IsDBNull(reader.GetOrdinal("PARAM5")) == false) 
         workpack.SPA = reader.GetString(reader.GetOrdinal("PARAM5")); 

        if (reader.IsDBNull(reader.GetOrdinal("PARAM6")) == false) 
         workpack.Writer = reader.GetString(reader.GetOrdinal("PARAM6")); 

        if (reader.IsDBNull(reader.GetOrdinal("PARAM7")) == false) 
         workpack.Organization = reader.GetString(reader.GetOrdinal("PARAM7")); 

        if (reader.IsDBNull(reader.GetOrdinal("PARAM8")) == false) 
         jobCardIDs.Add(reader.GetInt32(reader.GetOrdinal("PARAM8"))); 

       } 
       workpack.JobCardIDs = jobCardIDs; 
       return workpack; 
      } 
     } 
    } 
} 
0

受到社會各界給予未解決問題的以前的答案,雖然我沒有把額外的片斷吧。

問題出現在創建對象並在後貼之間傳遞時。該對象與選擇作業卡選項卡相關,但一旦加載的頁面沒有代碼來重建該實例。

在Page_Load(添加

 Object foo = new Object(); 

)和重建解決了該問題。如果任何人有任何建議,如何保持一個實例活着,我都聽到它。我認爲ViewState()和Session []是適用的方法。