473,395 Members | 1,474 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.

Help with testing DBNull from a field.

I have the following code:

Dim strQuery As String = "SELECT tblData.CoPayFundGrant,
tblData.M_DSS_SSA_Benefits, tblData.O_DSS_SSA_Benefits, " & _
"tblData.Ref_MPS, tblData.Ref_LA, tblData.Ref_DCMH,
tblData.Place_Comm, tblData.Rem_Comm, tblData.Core_Case_Man,
tblData.Ref_DCMH_Case_Man " & _
"FROM tblData;"
Dim rsDataManip As ADODB.Recordset = cnnJET.Execute(strQuery)
With rsDataManip
If Not .EOF Then
Dim NewDataValue As Object
For Each fldData As ADODB.Field In rsDataManip.Fields
Select Case fldData.Value.ToString ' ***** X
*****
Case "1 - Attempted-Successful"
NewDataValue = "Yes"
Case "2 - Attempted-Unsuccessful"
NewDataValue = "No"
Case Else
NewDataValue = System.DBNull.Value
End Select
fldData.Value = NewDataValue
NewDataValue = Nothing
Next
.MoveNext()

End If

End With

I had to change the line marked with "***** X *****" and add the ToString
so that I could test the field for string values and DBNull values. But I
want to test for string values and the actual DBNull value without changing
it to a string, so how can I change the code above to do this?
--
|
+-- Julian
|
+-- VB.Net 2003
|


Mar 26 '06 #1
2 2921
Julian wrote:
I have the following code:

Dim strQuery As String = "SELECT tblData.CoPayFundGrant,
tblData.M_DSS_SSA_Benefits, tblData.O_DSS_SSA_Benefits, " & _
"tblData.Ref_MPS, tblData.Ref_LA, tblData.Ref_DCMH,
tblData.Place_Comm, tblData.Rem_Comm, tblData.Core_Case_Man,
tblData.Ref_DCMH_Case_Man " & _
"FROM tblData;"
Dim rsDataManip As ADODB.Recordset = cnnJET.Execute(strQuery)
With rsDataManip
If Not .EOF Then
Dim NewDataValue As Object
For Each fldData As ADODB.Field In rsDataManip.Fields
Select Case fldData.Value.ToString ' ***** X
*****
Case "1 - Attempted-Successful"
NewDataValue = "Yes"
Case "2 - Attempted-Unsuccessful"
NewDataValue = "No"
Case Else
NewDataValue = System.DBNull.Value
End Select
fldData.Value = NewDataValue
NewDataValue = Nothing
Next
.MoveNext()

End If

End With

I had to change the line marked with "***** X *****" and add the ToString
so that I could test the field for string values and DBNull values. But I
want to test for string values and the actual DBNull value without changing
it to a string, so how can I change the code above to do this?


Select Case fldData.Value
Case is DBNull.Value
'DBNull
Case "1 - Attempted-Successful"
NewDataValue = "Yes"
Case "2 - Attempted-Unsuccessful"
NewDataValue = "No"
Case Else
NewDataValue = System.DBNull.Value
End Select
Mar 26 '06 #2
Because, the Type of fldData.Value os Object, you need to cast it as a
string before you can do string comparisons on it.

In my view it is best to check for DBNull first and then do whatever other
stuff you need to do. e.g.:

If fldData.Value Is DBNull.Value Then
...
Else
Select Case CType(fldData.Value, String)
Case "1 - Attempted-Successful"
NewDataValue = "Yes"
Case "2 - Attempted-Unsuccessful"
NewDataValue = "No"
Case Else
NewDataValue = DBNull.Value
End Select
End If

Note the use of CType(fldData.Value, String) instead of
fldData.Value.ToString. Under some circumstances the Object.ToString method
returns a value other than what is is expected whereas I have yet to
CType(<object>, String) return inappropriate values.
"Julian" <ad***@jdmils.com> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl...
I have the following code:

Dim strQuery As String = "SELECT tblData.CoPayFundGrant,
tblData.M_DSS_SSA_Benefits, tblData.O_DSS_SSA_Benefits, " & _
"tblData.Ref_MPS, tblData.Ref_LA, tblData.Ref_DCMH,
tblData.Place_Comm, tblData.Rem_Comm, tblData.Core_Case_Man,
tblData.Ref_DCMH_Case_Man " & _
"FROM tblData;"
Dim rsDataManip As ADODB.Recordset = cnnJET.Execute(strQuery)
With rsDataManip
If Not .EOF Then
Dim NewDataValue As Object
For Each fldData As ADODB.Field In rsDataManip.Fields
Select Case fldData.Value.ToString ' ***** X
*****
Case "1 - Attempted-Successful"
NewDataValue = "Yes"
Case "2 - Attempted-Unsuccessful"
NewDataValue = "No"
Case Else
NewDataValue = System.DBNull.Value
End Select
fldData.Value = NewDataValue
NewDataValue = Nothing
Next
.MoveNext()

End If

End With

I had to change the line marked with "***** X *****" and add the ToString
so that I could test the field for string values and DBNull values. But I
want to test for string values and the actual DBNull value without
changing it to a string, so how can I change the code above to do this?
--
|
+-- Julian
|
+-- VB.Net 2003
|

Mar 26 '06 #3

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

Similar topics

1
by: Gary | last post by:
In the following code the line: row("OrderDate") = DBNull is invalid. But, how can I set the field to DBNull? Thanks, Gary daClearOrders.Fill(DsClearOrders1) Dim row As DataRow
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...
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,...
4
by: Andrew Baker | last post by:
I have the following code that calles a stored proc in SQLServer. When the output parameter @custref is null (System.DBNull) I cant seem to find a test for this and I get an exception. I know I...
4
by: JohnR | last post by:
Hi, I'm trying to update a DBF file which I'm using VB.NET ODBC adapters, commands and connections. I fill the dataset and databind the columns to textboxes on my form. I can successfully view,...
3
by: Larry | last post by:
Hi all, I am coming over from VB 6, learning VB.NET. I have read and have in front of me, the language reference from the msdn, on; Behavior of Null has changed, Isdbnull function and...
3
by: keithb | last post by:
My code dynamically adds template fields to a GridView control. Everything seems to work OK, except when updating, because I haven't found a way to reference the dynamically added textboxes....
3
by: Finn Stampe Mikkelsen | last post by:
Hi I have defined a table in my database, with 2 date-fields. I have set a default value to DBNull. I have integrated a nullable datetimepicker control to my project and set the apropriate...
3
by: DragonLord | last post by:
Here is the situation I am inserting rows into a datagridview and then using a function to group similar rows together based on a column. When I call to compare the lastrow with the current row...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.