0
我正在處理新項目我必須從一個表讀取數據並從另一個表中讀取多個記錄並將它們保存在同一個表中目的。問題是,當我在閱讀器中讀取多行以獲得第二個查詢時,它工作正常,但是當我將它們分配給各自的對象變量時,它會給出對象引用錯誤。數據讀取器在從中提取數據時未將對象引用設置爲對象的實例
這是我的代碼。閱讀器的第一個實例只返回一個數據行,但第二個實例返回多個行。到目前爲止,我已經完成了一個閱讀器,然後再打開另一個閱讀器,以檢查它是否是線程問題,但現在正在使用。
public AdjustmentRequest GetAdjustmentRequestData(AdjustmentRequest Request)
{
AdjustmentRequest RequestData = new AdjustmentRequest();
using (OleDbConnection DbConnnection = new OleDbConnection(GetConnectionString()))
{
DbConnnection.Open();
//return list of all the Adjustment Categories
string query = "select * from AdjustmentRequests where RequestID ='" + Request.RequestID + "' and [DistributorCode] = '" + Request.DistributorCode + "' and [BranchCode] ='" + Request.BranchCode + "'";
OleDbCommand objOleDBcommand = new OleDbCommand(query, DbConnnection);
OleDbDataReader ojbOleDBReader = objOleDBcommand.ExecuteReader();
if (ojbOleDBReader.HasRows)
{
while (ojbOleDBReader.Read() == true)
{
RequestData.RequestID = ojbOleDBReader.GetValue(0).ToString();
RequestData.RequestStatus = ojbOleDBReader.GetValue(1).ToString();
RequestData.InvoiceNumber = ojbOleDBReader.GetValue(2).ToString();
RequestData.DistributorCode = ojbOleDBReader.GetValue(3).ToString();
RequestData.BranchCode = ojbOleDBReader.GetValue(4).ToString();
RequestData.Comments = ojbOleDBReader.GetValue(5).ToString();
RequestData.AdjustmentCategory = ojbOleDBReader.GetValue(6).ToString();
RequestData.UserID = Convert.ToInt32(ojbOleDBReader.GetValue(7));
}
}
else
{
RequestData = null;
}
if (DbConnnection != null)
{
ojbOleDBReader.Close();
}
string query1 = "select * from AdjustmentRequestDetails where RequestID ='" + Request.RequestID + "'";
OleDbCommand objOleDBcommandDetail = new OleDbCommand(query1, DbConnnection);
OleDbDataReader ojbOleDBReaderDetail = objOleDBcommandDetail.ExecuteReader();
if (RequestData != null)
{
if (ojbOleDBReaderDetail.HasRows)
{
while (ojbOleDBReaderDetail.Read() == true)
{
if (ojbOleDBReaderDetail.GetValue(2).ToString() == "Frait")
{
RequestData.RequestDetailFreight.RequestID = ojbOleDBReaderDetail.GetValue(0).ToString();
RequestData.RequestDetailFreight.AdjustmentCode = ojbOleDBReaderDetail.GetValue(1).ToString();
RequestData.RequestDetailFreight.AdjustmentCategory = ojbOleDBReaderDetail.GetValue(2).ToString();
RequestData.RequestDetailFreight.BrandCostCenter = ojbOleDBReaderDetail.GetValue(3).ToString();
RequestData.RequestDetailFreight.AmountRequested = ojbOleDBReaderDetail.GetValue(4).ToString();
RequestData.RequestDetailFreight.AmountApproved = ojbOleDBReaderDetail.GetValue(5).ToString();
RequestData.RequestDetailFreight.Status = ojbOleDBReaderDetail.GetValue(6).ToString();
RequestData.RequestDetailFreight.Assignee = ojbOleDBReaderDetail.GetValue(7).ToString();
RequestData.RequestDetailFreight.UpdatedDate = ojbOleDBReaderDetail.GetValue(8).ToString();
RequestData.RequestDetailFreight.UpdatedBy = ojbOleDBReaderDetail.GetValue(9).ToString();
RequestData.RequestDetailFreight.ApproveRejectDate = ojbOleDBReaderDetail.GetValue(10).ToString();
RequestData.RequestDetailFreight.RejectionCode = ojbOleDBReaderDetail.GetValue(11).ToString();
}
if (ojbOleDBReaderDetail.GetValue(2).ToString() == "Handling")
{
RequestData.RequestDetailHandling.RequestID = ojbOleDBReaderDetail.GetValue(0).ToString();
RequestData.RequestDetailHandling.AdjustmentCode = ojbOleDBReaderDetail.GetValue(1).ToString();
RequestData.RequestDetailHandling.AdjustmentCategory = ojbOleDBReaderDetail.GetValue(2).ToString();
RequestData.RequestDetailHandling.BrandCostCenter = ojbOleDBReaderDetail.GetValue(3).ToString();
RequestData.RequestDetailHandling.AmountRequested = ojbOleDBReaderDetail.GetValue(4).ToString();
RequestData.RequestDetailHandling.AmountApproved = ojbOleDBReaderDetail.GetValue(5).ToString();
RequestData.RequestDetailHandling.Status = ojbOleDBReaderDetail.GetValue(6).ToString();
RequestData.RequestDetailHandling.Assignee = ojbOleDBReaderDetail.GetValue(7).ToString();
RequestData.RequestDetailHandling.UpdatedDate = ojbOleDBReaderDetail.GetValue(8).ToString();
RequestData.RequestDetailHandling.UpdatedBy = ojbOleDBReaderDetail.GetValue(9).ToString();
RequestData.RequestDetailHandling.ApproveRejectDate = ojbOleDBReaderDetail.GetValue(10).ToString();
RequestData.RequestDetailHandling.RejectionCode = ojbOleDBReaderDetail.GetValue(11).ToString();
}
if (ojbOleDBReaderDetail.GetValue(2).ToString() == "Shipping Charges")
{
RequestData.RequestDetailShippingCharges.RequestID = ojbOleDBReaderDetail.GetValue(0).ToString();
RequestData.RequestDetailShippingCharges.AdjustmentCode = ojbOleDBReaderDetail.GetValue(1).ToString();
RequestData.RequestDetailShippingCharges.AdjustmentCategory = ojbOleDBReaderDetail.GetValue(2).ToString();
RequestData.RequestDetailShippingCharges.BrandCostCenter = ojbOleDBReaderDetail.GetValue(3).ToString();
RequestData.RequestDetailShippingCharges.AmountRequested = ojbOleDBReaderDetail.GetValue(4).ToString();
RequestData.RequestDetailShippingCharges.AmountApproved = ojbOleDBReaderDetail.GetValue(5).ToString();
RequestData.RequestDetailShippingCharges.Status = ojbOleDBReaderDetail.GetValue(6).ToString();
RequestData.RequestDetailShippingCharges.Assignee = ojbOleDBReaderDetail.GetValue(7).ToString();
RequestData.RequestDetailShippingCharges.UpdatedDate = ojbOleDBReaderDetail.GetValue(8).ToString();
RequestData.RequestDetailShippingCharges.UpdatedBy = ojbOleDBReaderDetail.GetValue(9).ToString();
RequestData.RequestDetailShippingCharges.ApproveRejectDate = ojbOleDBReaderDetail.GetValue(10).ToString();
RequestData.RequestDetailShippingCharges.RejectionCode = ojbOleDBReaderDetail.GetValue(11).ToString();
}
if (ojbOleDBReaderDetail.GetValue(2).ToString() == "Other")
{
RequestData.RequestDetailOther.RequestID = ojbOleDBReaderDetail.GetValue(0).ToString();
RequestData.RequestDetailOther.AdjustmentCode = ojbOleDBReaderDetail.GetValue(1).ToString();
RequestData.RequestDetailOther.AdjustmentCategory = ojbOleDBReaderDetail.GetValue(2).ToString();
RequestData.RequestDetailOther.BrandCostCenter = ojbOleDBReaderDetail.GetValue(3).ToString();
RequestData.RequestDetailOther.AmountRequested = ojbOleDBReaderDetail.GetValue(4).ToString();
RequestData.RequestDetailOther.AmountApproved = ojbOleDBReaderDetail.GetValue(5).ToString();
RequestData.RequestDetailOther.Status = ojbOleDBReaderDetail.GetValue(6).ToString();
RequestData.RequestDetailOther.Assignee = ojbOleDBReaderDetail.GetValue(7).ToString();
RequestData.RequestDetailOther.UpdatedDate = ojbOleDBReaderDetail.GetValue(8).ToString();
RequestData.RequestDetailOther.UpdatedBy = ojbOleDBReaderDetail.GetValue(9).ToString();
RequestData.RequestDetailOther.ApproveRejectDate = ojbOleDBReaderDetail.GetValue(10).ToString();
RequestData.RequestDetailOther.RejectionCode = ojbOleDBReaderDetail.GetValue(11).ToString();
}
}
}
}
if (DbConnnection != null)
{
ojbOleDBReaderDetail.Close();
DbConnnection.Close();
}
}
return RequestData;
}
是'RequestData.RequestDetailFreight'而不是'NULL'? – Shyju