Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How do I deal with null values in tables? If i'm getting information from a table (all over the program) and in one record in one field might be a random DBNULL, whats the best way to cast this into a string?

 

"Cast from type 'DBNull' to type 'String' is not valid."

 

 

line of code:

 

sTemp2 = esEmpTable.Rows(0).Item("middle name")

www.DRSTEIN99.com www.RAIDGEAR.net www.THERE.com -> Tell them DrStein99 sent ya!
  • *Experts*
Posted (edited)
If (esEmpTable.Rows(0).Item("middle name")GetType.ToString)<> "System.DBNull" Then
sTemp2 = cstr(esEmpTable.Rows(0).Item("middle name"))
Else: sTemp2 = "No value set"
End If

Edited by jfackler
  • *Experts*
Posted
A possibly more efficient way to do it (plus shorter) is like this:
If esEmpTable.Rows(0).Item("middle name") <> Convert.DBNull Then sTemp2 = esEmpTable.Rows(0).Item

I would avoid string comparison wherever possible due to the terrible inefficiency and speed issues.

  • *Experts*
Posted

Slightly off-topic:

I've never seen Convert.DBNull - is that VB-specific, or just another way to get at DBNull? In C# it's System.DBNull.Value.

 

I'm just wondering if it's like int and Int32 in C# or Integer and Int32 in VB.NET (the same object, just used two different ways - or 4 if you count different languages).

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted
I'm using it in my C# app right now, so it's not VB specific. I think the idea is that it represents an object rather than a value, so it can be used as such.
Posted

Here's the way I prefer to check this, for what its worth:

 

<vb>

if ISDBNull(esEmpTable.Rows(0).Item("middle name")) then sTemp2 = esEmpTable.Rows(0).Item

</vb>

  • *Experts*
Posted

Now that one is VB.NET specific. As a general rule, if you are using a standalone function (i.e. ThisFunction(params) rather than Class.ThisFunction(params)) that is built in to the framework, there is a good chance it is for VB6 compatability. With .NET, almost all functions are built into the classes they modify: stringobject.IndexOf() has replaced InStr(), for example. Nothing stands out on its own except the VB6 compatability library.

 

I really hope Microsoft does away with it in the next .NET, though I really don't see it happening.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...