Cannot cast DBNull.Value to System.Decimal error in LINQ

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi guys.
I have a dataTable with values like so:
<table border="0" cellpadding="0" cellspacing="0" width="192" style="width:144pt; border-collapse:collapse
<colgroup><col span="3" width="64" style="width:48pt </colgroup>
<tbody>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt

CITY
</td>
<td width="64" style="border-width:48pt ZIP</td>
<td width="64" style="border-width:48pt ON_PEAK</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt SAN DIEGO</td>
<td width="64" style="border-width:48pt 92124</td>
<td width="64" style="border-width:48pt 0.25</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt ESCONDIDO</td>
<td width="64" style="border-width:48pt 92025</td>
<td width="64" style="border-width:48pt 0.25</td>
</tr>
<tr height="31" style="height:23.25pt
<td height="31" width="64" style="width:48pt; height:23.25pt RANCHO LA COSTA</td>
<td width="64" style="border-width:48pt 92009</td>
<td width="64" style="border-width:48pt 0.25</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt SAN DIEGO</td>
<td width="64" style="border-width:48pt 92107</td>
<td width="64" style="border-width:48pt 0.25</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt SAN DIEGO</td>
<td width="64" style="border-width:48pt 92122</td>
<td width="64" style="border-width:48pt 0.25</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt POWAY</td>
<td width="64" style="border-width:48pt 92064</td>
<td width="64" style="border-width:48pt 0.26</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt AGUANGA</td>
<td width="64" style="border-width:48pt 92536</td>
<td width="64" style="border-width:48pt NULL</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt AGUANGA</td>
<td width="64" style="border-width:48pt 92536</td>
<td width="64" style="border-width:48pt NULL</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt ALISO VIEJO</td>
<td width="64" style="border-width:48pt 92656</td>
<td width="64" style="border-width:48pt NULL</td>
</tr>
<tr height="20" style="height:15pt
<td height="20" width="64" style="width:48pt; height:15pt ALISO VIEJO</td>
<td width="64" style="border-width:48pt 92656</td>
<td width="64" style="border-width:48pt NULL</td>
</tr>
</tbody>
</table>

Im trying to write a LINQ query to pull out rows that have values ON_PEAK >= 0.25
The problem is the column "ON_PEAK" is of type decimal, but I allowed NULLs in the database. So when I write a LINQ query like so:
<pre class="prettyprint var onPeak =
from t in tempData.Tables[queryTable].AsEnumerable()
where (t.Field<decimal>("ON_PEAK")>= 0.25
select new { CITY = t.field<string>("CITY"),
PEAK_P = t.field<decimal>("ON_PEAK")
};[/code]
<br/>
It gives me a very annoying error that I cant solve in the debugger:
{"Cannot cast DBNull.Value to type System.Decimal. Please use a nullable type."} System.SystemException {System.InvalidCastException}
Can someone help? Is there a way to check a table cell that is strongly typed? It is giving me a headache.

View the full article
 
Back
Top