title: Data is Null. This method or property cannot be called on null value after joining two tables in C#.net
body: <p>After I joined the table in my codes the error "Data is Null. This method or property cannot be called on null value " occurred. </p>
  
  <p>Please have a look on my codes and please help. I tried to look for the answer in the google but still didn't get what I wanted to do. </p>
  
  <p>Here's my codes which causes the  mentioned error:</p>
  
  <pre><code>private static string m_sConnectionString = ConfigurationManager.ConnectionStrings["NomsConnection"].ConnectionString;
  private static string
      m_sReport = "SELECT r.[RequestID],r.[RequestDate],r.[PARNumber],r.[StatusID],r.[PurchaseComment]"   // 0 - 4
                  + ",r.[UID],r.[MyUID],r.[FullName],r.[Email]"                                         // 5 - 8
                  + ",r.[EntityName],r.[DepartmentName],r.[DepartmentID]"                                 // 9 - 11
                  + ",r.[LastBy]"                                                                         // 12
                  + ",r.[ProgramID],r.[ProgramCode],r.[ProgramName],r.[CostCenterCode]"                   // 13 - 16
                  + ",p.[PartDesc],p.[SupplierID],p.[AccountType],p.[CurrName],p.[PartQuantity],p.[PiecePrice]"
                  + "FROM [NOP_PR].[dbo].[Requests] r "
                  + "JOIN [NOP_PR].[dbo].[Parts] p on p.[RequestID] = r.[RequestID]"
                  + "WHERE [CountryName] IN ('Philippines')";
  
  
  
  
   public static List&lt;NomsPRRequest&gt; LoadPRRequestFromDB(DateTime from, DateTime to) {
              string sSrcipt = m_sReport + "and [RequestDate] between '" + from.ToString("yyyy-MM-dd HH:mm:ss") + "' and '" + to.ToString("yyyy-MM-dd HH:mm:ss") + "'";
              Dictionary&lt;long, NomsPRRequest&gt; data = new Dictionary&lt;long, NomsPRRequest&gt;();
              long key;
              double dAmount;
  
          using (SqlConnection con = new SqlConnection(m_sConnectionString)) {
              con.Open();
  
              using (SqlCommand command = new SqlCommand(sSrcipt, con)) {
                  SqlDataReader reader = command.ExecuteReader();
                  while (reader.Read()) {
                      key = reader.GetInt64(0);
                      if (!data.ContainsKey(key)) {
                          data.Add(key, new NomsPRRequest() {
                              RequestID = key
                              ,RequestDate = reader.GetDateTime(1)
                              ,PARNumber = reader.GetString(2)
                              ,StatusID = reader.GetInt64(3)
                              ,FullName = reader.GetString(7)
                              ,LastBy = reader.GetString(12)
                              ,ProgramName = reader.GetString(14)
                              ,ItemList = new List&lt;NomsPRItem&gt;()
                              ,TotalAmount = 0.0
                          });
                      }
  
                      dAmount = (double)reader.GetDecimal(21) * (double)reader.GetDecimal(22);
  
                      data[key].TotalAmount += dAmount;
  
                       data[key].ItemList.Add(new NomsPRItem() {
                          RequestID = key,
                          PartDesc = reader.GetString(17)
                          ,SupplierID = reader.GetString(18)
                          ,AccountType = reader.GetString(19)
                          ,CurrName = reader.GetString(20)
                          ,PartQuantity = (double)reader.GetDecimal(21)
                          ,PiecePrice = (double)reader.GetDecimal(22)
                          ,Amount = dAmount
  
                      });
                  }
              }
          }
  
          return data.Values.ToList();
  
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Since the column [LastBy] in my database only has the null values .. I just added what Luann said :) .. Thanks for sharing Luann !</p>
  
  <pre><code> data[key].ItemList.Add(new NomsPRItem() {
      ,RequestDate = reader.GetDateTime(1)
                  ,PARNumber = reader.GetString(2)
                  ,StatusID = reader.GetInt64(3)
                  ,FullName = reader.GetString(7)
                  ,LastBy = reader.IsDBNull(12) ? null : reader.GetString(12)
                  ,ProgramName = reader.GetString(14)
                  ,ItemList = new List&lt;NomsPRItem&gt;()
                  ,TotalAmount = 0.0
  
  });
  </code></pre>