473,395 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to easily parse DBNull value into other data type

Hi

This is a newbie question.
I've a datatable that contains null value. Sometime i need to store this
data into different variables. However, error will be thrown if the data
i tried to stored in the variable is a null value.

E.g.
Dim int As Int64
Dim null As Object
null = DBNull.Value
int = null 'this will throw error

To solve this, i have to check if the value is DBNull each time. Is
there a simpler way to do this?

Thanks in advance.

From,
Hon Yuen, Ng
Nov 21 '05 #1
4 5736
I usually create a function like this:

Function SafeString(ByVal o As Object) As String
Dim s As String = String.Empty
If Not (o Is Nothing OrElse o Is DBNull.Value) Then
s = Convert.ToString(o)
End If
Return (s)
End Function

This function returns an empty string if you pass it an DBNull.Value. Also
note that the object is first checked to make sure it is not Nothing. If
you're pulling data from a SQL Server, you can use the COALESCE() function
on the Server-Side to perform a similar function before it ever gets to your
client app.

Thanks,
Mike C

"Hon Yuen, Ng" <hy**@tm.net.my> wrote in message
news:eA**************@TK2MSFTNGP14.phx.gbl...
Hi

This is a newbie question.
I've a datatable that contains null value. Sometime i need to store this
data into different variables. However, error will be thrown if the data i
tried to stored in the variable is a null value.

E.g.
Dim int As Int64
Dim null As Object
null = DBNull.Value
int = null 'this will throw error

To solve this, i have to check if the value is DBNull each time. Is there
a simpler way to do this?

Thanks in advance.

From,
Hon Yuen, Ng

Nov 21 '05 #2
P.S. - For Numeric values you can use a similar function, but return a 0 for
DBNull.Value or Nothing.

Thanks,
Mike C.

"Hon Yuen, Ng" <hy**@tm.net.my> wrote in message
news:eA**************@TK2MSFTNGP14.phx.gbl...
Hi

This is a newbie question.
I've a datatable that contains null value. Sometime i need to store this
data into different variables. However, error will be thrown if the data i
tried to stored in the variable is a null value.

E.g.
Dim int As Int64
Dim null As Object
null = DBNull.Value
int = null 'this will throw error

To solve this, i have to check if the value is DBNull each time. Is there
a simpler way to do this?

Thanks in advance.

From,
Hon Yuen, Ng

Nov 21 '05 #3
Thanks for your reply.
I think i understood what you mentioned. However, what if i do data
binding? If i bind a data table to a TextBox, and the data is a
DBNull.Value? What is the best practice to solve this kind of problem?

Thanks in advance.

From,
Hon Yuen, Ng
Nov 21 '05 #4
Hon,

For that are the binding events

In this message is a sample from me that I made for this.

http://groups-beta.google.com/group/...f8d2a7210eb1c2

I hope this helps?

Cor
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
0
by: Dr. Paul Caesar - CoullByte (UK) Limited | last post by:
Hi all! I have a windows form that is bound to a dataset. Using VS 2005 & .NET Framework Beta 2. In my dataset I have a colum called Warranty that is a smallint on SQL Server 2000(SP4). ...
7
by: | last post by:
Source Error: Line 173: sData(rownumber - 1, lcnt) = WhatCol.Value Line 174: End IF Line 175: If (sData(rownumber, lcnt) = sData(rownumber - 1, lcnt)) AND...
1
by: fredda054 | last post by:
Hi everybody ! With help from some nice people here I'm getting close to solve my problem with dbnull values in a repeater. I got it pretty much done, I just ned to fix some syntax. I get a...
4
by: Dursun | last post by:
Hi, I am trying to assign NULL to a datetime field in the SQL Server database. Here is the code that does NOT work: INSERT INTO ... .... VALUES ... .... CType(IIf(dateWitness2Date.Checked,...
6
by: Charlie Brown | last post by:
When checking for NULL values from a database, normally I would use If Not row("NetSales") Is DBNull.Value Then NetSales = row("NetSales") End If However, if I use a typed dataset then I...
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
9
by: Robert Bravery | last post by:
HI all, I have a column value returned to a string variable in my c# app. But the return type is of system.dbnull. How can I convert that to a system.sting Thanks Robert
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.