我从以下代码中收到错误,该代码是为show table data
编写的数据是空的。无法在Null值上调用此方法或属性。
所以这是我的代码,以便您可以帮我调查。
public static List<NomsPRRequest> LoadPRfromDB_withParams(DateTime from, DateTime to, string EntityID,
string DepartmentID)
{
string sScript = m_sReport + ((EntityID == "") ? "" : " AND d.[EntityID]=" + EntityID) + ((DepartmentID == "") ? "" : " AND d.[DepartmentID]=" + DepartmentID)
+ " 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(sScript, 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),
DepartmentName = reader.GetString(10),
DepartmentID = reader.GetInt64(11),
StatusID = reader.GetInt64(3),
FullName = reader.GetString(7),
InboxLearUID = 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),
PartNumber = reader.GetString(23),
SupplierID = reader.GetString(18),
FullName = reader.GetString(7),
AccountType = reader.GetString(19),
CurrName = reader.GetString(20),
PartQuantity = (double)reader.GetDecimal(21),
PiecePrice = (double)reader.GetDecimal(22),
Amount = dAmount
});
}
}
}
return data.Values.ToList();
}
我在这部分得到错误
data [key] .ItemList.Add(new NomsPRItem()
这是我的观点..
<table data-ng-show="models != null" class="table table-striped table-bordered table-hover"
id="PRTable" >
<tr class="titlerow">
<th>
<a href="#" data-ng-click="sorting='RequestDate'; reverse = !reverse">PR Date <span
data-ng-show="sorting == 'RequestDate'"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='RequestID '; reverse = !reverse">PR # <span data-ng-show="sorting == 'RequestID '"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='PARNumber '; reverse = !reverse">PAR # <span
data-ng-show="sorting == 'PARNumber '"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='ProgramName '; reverse = !reverse">Program <span
data-ng-show="sorting == 'ProgramName '"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='FullName '; reverse = !reverse">Requestor <span
data-ng-show="sorting == 'FullName '"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='DepartmentName '; reverse = !reverse">Department <span
data-ng-show="sorting == 'FullName '"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='PONo'; reverse = !reverse">PO #
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='StatusID '; reverse = !reverse">PRStatus<span
data-ng-show="sorting == 'StatusID '"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='Amount '; reverse = !reverse">Total Amount<span
data-ng-show="sorting == 'Amount '"></span>
</a>
</th>
<th>
<a href="#" data-ng-click="sorting='InboxLearUID '; reverse = !reverse">Last Action<span
data-ng-show="sorting == 'InboxLearUID '"></span>
</a>
</th>
</tr>
<tr data-ng-repeat="model in models | orderBy: sorting:reverse | filter : filterAllColumns | filter : filterOptions ">
<td>{{jsonDatetotext(model.RequestDate) | date:'MM/dd/yyyy'}}</td>
<td>
<a href="#" data-toggle="modal" data-target="#basicModalContent" data-ng-click="getSelectedPR(model)">{{model.RequestID}}
</a>
</td>
<td>{{model.PARNumber }}</td>
<td>{{model.ProgramName }}</td>
<td>{{model.FullName }}</td>
<td>{{model.DepartmentName | uppercase}}</td>
<td>{{model.PONo}}</td>
<td>{{StatusList[model.StatusID] | uppercase}}</td>
<td class="totalAmount"><span class="pull-right">{{model.TotalAmount | number:2}}</span>
</td>
<td>{{model.InboxLearUID | lowercase}}</td>
</tr>
</table>
</div>
<!-- /.Modal Na ni -->
<div class="modal fade" id="basicModalContent" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body" id="exportablePRItems">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Item Code
</th>
<th>Item Description
</th>
<th>Supplier
</th>
<th>Account
</th>
<th>Currency
</th>
<th>Amount
</th>
<th>(USD) Amount
</th>
</tr>
</thead>
<tbody data-ng-repeat="selectedPR in selectedModal.ItemList">
<tr>
<td>{{selectedPR.PartNumber}}</td>
<td>{{selectedPR.PartDesc}}</td>
<td>{{selectedPR.SupplierID }}</td>
<td>{{selectedPR.AccountType}}</td>
<td>{{selectedPR.CurrName }}</td>
<td data-ng-model="amount" class="amount">{{selectedPR.Amount | number:2}}</td>
<td>{{selectedPR.AmountUSD}}</td>
</tr>
</tbody>
<tr>
<td><span class="pull-right"><i class="glyphicon glyphicon-plus-sign"></i></span></td>
<td colspan="3"><b>{{selectedModal.RequestID}}</b> </td>
<td colspan="1"><b>Total : </b></td>
<td colspan="2">{{selectedModal.ItemList | sumbykey : 'Amount' | number:2}}</td>
</tr>
</table>
</div>
<footer>
<br />
<button data-ng-click="exportDataItems()" class="btn btn-warning"><i class="glyphicon glyphicon-export"></i>Export Item </button>
</footer>
</div>
<div class="modal-footer">
<button id="btnModalCancel" type="button" class="btn btn-default" data-dismiss="modal">
Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
任何人都可以帮我调查一下这些代码,以便我可以继续我的下一步吗?
答案 0 :(得分:2)
我认为问题在于将十进制类型转换为double的行。 如果21和22的值返回DbNull,则无法将该类型转换为double。我建议:
或者,您可以通过将对象初始化代码提取到较小的部分来进行一些调试:
var npri = new NomsPRItem();
npri.RequestID = key;
npri.PartDesc = reader.GetString(17);
npri.PartNumber = reader.GetString(23);
npri.SupplierID = reader.GetString(18);
npri.FullName = reader.GetString(7);
npri.AccountType = reader.GetString(19);
npri.CurrName = reader.GetString(20);
npri.PartQuantity = (double)reader.GetDecimal(21);
npri.PiecePrice = (double)reader.GetDecimal(22);
npri.Amount = dAmount;
这将精确指出导致问题的列索引。
答案 1 :(得分:1)
更好地使用IsDBNull ..就像这样:
RequestID = key,
PartDesc = reader.IsDBNull(17) ? null : reader.GetString(17),
PartNumber = reader.IsDBNull(23) ? null : reader.GetString(23),
SupplierID = reader.IsDBNull(18) ? null : reader.GetString(18),
FullName = reader.IsDBNull(7) ? null : reader.GetString(7),
AccountType = reader.IsDBNull(19) ? null : reader.GetString(19),
CurrName = reader.IsDBNull(20) ? null : reader.GetString(20),
PartQuantity = (double)reader.GetDecimal(21),
PiecePrice = (double)reader.GetDecimal(22),
Amount = dAmount
我希望它可以提供帮助