DBNull to String???

DBNull to String???

Post by [Corpuslogic] Thomas U Nielse » Sat, 08 Mar 2003 23:47:02

It works.

> > I've tried things like lblWhatever.text =
> > dtrWhatever("colname").tostring() but can't seem to get this to work.

> A trick that worked VB6 may work here, but I've not tested it:

> lblWhatever.text = dtrWhatever("colname") & ""

> Chris

> --
> If you don't like lunchmeat, please remove it from my e-mail address to
> send me an e-mail


1. DBNull To String

Using VS.Pro, VB.Net, ADO.NET, SQL native...

Three Questions:

Question 1) The following code will fail if the DataSet Column is Null:
dim x as string
x = DataSet.TableName(RowNumber).ColumnName

To be safe, this has caused me to do the following, which works fine:
dim x as string
if not DataSet.TableName(RowNumber).IsColumnNameNull then
    x = DataSet.TableName(RowNumber).ColumnName
    x = ""
end if

This is a lot of extra code, when all I want is "" in a string for a Null in
the DataSet.  Is there an easier way to do it?  Something like the following
would be far easier, but does not work:
 x = trim(DataSet.TableName(RowNumber).ColumnName)

Question 2) Also, I would expect the FIRST catch in the following to catch
the error above, it does not.  Why?  How would I write it to catch this
SPECFIC error?  I am using System.InvalidCastException because that is the
error thrown in SECOND catch (i.e. exp) below.

dim x as string
     x = DataSet.TableName(RowNumber).ColumnName
catch e as System.InvalidCastException
    ' catch error moving Null to String
catch exp as Exception
    x = ""
end try

Question 3) I am creating the entire DataSource, so could configure it the
way I want.  I started out non allowing any nulls in any columns, but found
that took a lot of extra code.  I switched to allowing nulls in all but
index columns, but now realize there is going to be a lot of work checking
for nulls to prevent the problem above.   Is there a best practice in this
situation?  Is it better to deal with the problem on the front end (not
allow nulls) or on the back end (allow nulls, but you must check if it is


2. share AS5300 or VOIP with me!

3. cast from DBNull to string is not valid

4. Newsbase not expiring!

5. Returning DBNull to String Column of TypedDataset

6. Cable modem Intranet/home network

7. Cast from type dbnull to string is not valid

8. HP ANSI C vs HP aC++ - for ANSI C

9. DBNull to String???

10. error cast DBNull to string

11. Invalid Cast from DBNull to String

12. Invalid cast from DBNull to String

13. Cast from DBNull to String is not valid