Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - DBNull check for ADODB.recordset

Reply
 
Old 04-08-2008   #1 (permalink)
Mike OKC


 
 

DBNull check for ADODB.recordset

VB 2005

All developers face this issue; so I'm sure Microsoft was a solution for it.

What is the built in method to check DBNull in a recordset field? NOT a
typed dataset.
I don't want to modify my sql script because it is created dynamically.
Below I have the old method to check for DBNull and to deal with DBNull in
the recordset. This method of coding is used over 80 times as I have over 80
fields to check for DBNull, so a one line method call off the value recordset
would be great.

If IsDBNull(rstSQLdetail.Fields("CaseNumber").Value) Then
txtCaseNumber.Text = ""
Else
txtCaseNumber.Text = rstSQLdetail.Fields("CaseNumber").Value
End If


It would be great to have something LIKE this,

txtCaseNumber.Text = rstSQLdetail.Fields("CaseNumber").Value.ChkDBNull("")

In this example the method would return a zero length string.



My System SpecsSystem Spec
Old 04-08-2008   #2 (permalink)
Stephany Young


 
 

Re: DBNull check for ADODB.recordset

Simply write yourself a function like:

Function ChkDBNull(ByVal value As Object) As String

If IsDbNull(value) Then Return String.Empty

Return value.ToString()

End Function

and then your 'check' becomes something like:

txtCaseNumber.Text = ChkDBNull(rstSQLdetail.Fields("CaseNumber").Value)

but .... DbNull.Value.ToString() returns an empty string, so, instead, you
can simply use:

txtCaseNumber.Text = rstSQLdetail.Fields("CaseNumber").Value.ToString()

The big gotcha of course is when you start dealing with column types that
don't represent a string per se, like datetime, bit, int, image, etc. You're
goning to have to figure out how to deal with the law of unintended
consequences.


"Mike OKC" <MikeOKC@xxxxxx> wrote in message
news:81D5FC48-F0EE-45DC-9AA7-14CA25340C60@xxxxxx
Quote:

> VB 2005
>
> All developers face this issue; so I'm sure Microsoft was a solution for
> it.
>
> What is the built in method to check DBNull in a recordset field? NOT a
> typed dataset.
> I don't want to modify my sql script because it is created dynamically.
> Below I have the old method to check for DBNull and to deal with DBNull in
> the recordset. This method of coding is used over 80 times as I have over
> 80
> fields to check for DBNull, so a one line method call off the value
> recordset
> would be great.
>
> If IsDBNull(rstSQLdetail.Fields("CaseNumber").Value) Then
> txtCaseNumber.Text = ""
> Else
> txtCaseNumber.Text =
> rstSQLdetail.Fields("CaseNumber").Value
> End If
>
>
> It would be great to have something LIKE this,
>
> txtCaseNumber.Text = rstSQLdetail.Fields("CaseNumber").Value.ChkDBNull("")
>
> In this example the method would return a zero length string.
>
>
My System SpecsSystem Spec
Old 04-17-2008   #3 (permalink)
Leon Mayne


 
 

Re: DBNull check for ADODB.recordset

"Mike OKC" <MikeOKC@xxxxxx> wrote in message
news:81D5FC48-F0EE-45DC-9AA7-14CA25340C60@xxxxxx
Quote:

> VB 2005
>
> All developers face this issue; so I'm sure Microsoft was a solution for
> it.
>
> What is the built in method to check DBNull in a recordset field? NOT a
> typed dataset.
> I don't want to modify my sql script because it is created dynamically.
> Below I have the old method to check for DBNull and to deal with DBNull in
> the recordset. This method of coding is used over 80 times as I have over
> 80
> fields to check for DBNull, so a one line method call off the value
> recordset
> would be great.
>
> If IsDBNull(rstSQLdetail.Fields("CaseNumber").Value) Then
> txtCaseNumber.Text = ""
> Else
> txtCaseNumber.Text =
> rstSQLdetail.Fields("CaseNumber").Value
> End If
>
>
> It would be great to have something LIKE this,
>
> txtCaseNumber.Text = rstSQLdetail.Fields("CaseNumber").Value.ChkDBNull("")
>
> In this example the method would return a zero length string.
I just wrote an article about this:
http://leon.mvps.org/DotNet/CheckDbNull.html

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
run an UPDATE query on recordset VB Script
Checking for DBNull with generics .NET General
Difficulties using ADODB.recordset in powershell PowerShell
Using a variable in a ADO recordset PowerShell
ADO recordset from standard in/out? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46