Casting a DBNull

N

.Net Sports

I'm trying to match fields from XML feed to a datarow, but I get a
"System.InvalidCastException: Object cannot be cast from DBNull to
other types" error at the PkgAmount line. This code was converted from
original C# code, so I'm wondering if it has something to do with C#'s
strict datatype criteria (even tho this is in vb.net), or I need to do
something extra around the
FormatAmount(Convert.ToSingle(drwData("PkgAmount"))) ...
'''''''''''''''''
Dim drwData As DataRow
For Each drwData In dtaData.Rows
Dim SID As String = "SID=" & Convert.ToString(drwData("ID")) & "&"
Dim PurchaseTypeID As String = Convert.ToString(drwData("PurchaseID"))
Dim PkgAmount As String =
FormatAmount(Convert.ToSingle(drwData("PkgAmount")))
'''''''''''''''''''''
TIA
netsports
 
C

Cor Ligthert [MVP]

Hi

Why not write it in VB.Net code (However, you can in C# as well not use a
string as a single)

Altough it is a little bit strange code, alone the last line will fill the
items which are inside the method so those can never be used.

Dim drwData As DataRow
For Each drwData In dtaData.Rows
Dim SID As String = SID=" & drwData("ID").ToString & "&"
Dim PurchaseTypeID As String = drwData("PurchaseID").ToString
Dim PkgAmount As Single = Csng(drwData("PkgAmount"))
''Or just as the other strings,

I hope this helps,

Cor
 
D

David Hubbard

I am not sure if this helps but in C# if you have a nullable value say a
birthday that can be read from db then you would use


DateTime? fBirthday;

// read from db
...

// save as string

String tmp = ""

if ( fBirthday != null )
{
tmp = ( (DateTime) fBirthday ).Date;
}

Sorry but I don't know vb, but I have to do similiar things for columns that
can be null in some tables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top