After I joined the table in my codes the error "Data is Null. This method or property cannot be called on null value " occurred.
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.
Here's my codes which causes the mentioned error:
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<NomsPRRequest> 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<long, NomsPRRequest> data = new Dictionary<long, NomsPRRequest>();
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<NomsPRItem>()
,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();
}
}
Since the column [LastBy] in my database only has the null values .. I just added what Luann said :) .. Thanks for sharing Luann !
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<NomsPRItem>()
,TotalAmount = 0.0
});