473,385 Members | 1,863 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,385 software developers and data experts.

properly passing DBNull.value to DB via a variable

A common problem I encounter is having to enter either a date OR a null
value into a date field in a DB.

The problem I hace is that DBNull.value isn't a proper value for a date
variable, so I can't explicitely declare the variable:

Dim myDateVariable
If Trim(tbx_postDate.Text.ToString) <"" Then
myDateVariable= CType(tbx_postDate.Text.ToString, Date)
Else
myDateVariable= DBNull.Value
End If

Is that OK to not specifically declare the datatype for myDateVariable? Is
there a more elegant way to handle this?

-Darrel
Dec 12 '06 #1
1 1883
It's a bad problem with no great solutions. Nullable types in 2.0 _should_
solve the problem, except a null nullable type does NOT map to DbNull - what
were they thinking?

Anyways, that point asside, I generally just use a helper method:

command.Parameters.Add("@Data", SqlDbType.DateTime).Value =
SanitizeDateTime(dateValue)

public shared function SanitizeDateTime(string dateStringValue) as object
if...
return the date
end if
return DBNull.Value
end function

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"darrel" <no*****@nowhere.comwrote in message
news:OZ**************@TK2MSFTNGP03.phx.gbl...
>A common problem I encounter is having to enter either a date OR a null
value into a date field in a DB.

The problem I hace is that DBNull.value isn't a proper value for a date
variable, so I can't explicitely declare the variable:

Dim myDateVariable
If Trim(tbx_postDate.Text.ToString) <"" Then
myDateVariable= CType(tbx_postDate.Text.ToString, Date)
Else
myDateVariable= DBNull.Value
End If

Is that OK to not specifically declare the datatype for myDateVariable? Is
there a more elegant way to handle this?

-Darrel
Dec 12 '06 #2

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

Similar topics

5
by: amber | last post by:
Hello, I'm not sure if this question should go in the SQL forum or this one... I'm using VB.NET and SQL Server. I'm using stored procedures to update and create new records in my database. On...
5
by: Mark | last post by:
Assume that "some_app_setting" does not exist in my web.config file. Then, the first line of code below assigns the value null to strApplication. Why doesn't my IF statement return true, and...
1
by: John Hoge | last post by:
Is it possible to pass a null value to a stored procedure in .net? I have a search Sproc that can take one of two numbers to search on, but not both. I use the code below to pass a null value to...
1
by: Marty U. | last post by:
I have a form that has several fields that need to update a db record using SQL Data Provider. What is the best practice to insert DBNull if a text field is empty? I normally use if (txtbox.text...
1
by: Craig G | last post by:
how do you pass a null into an oracle parameter? i am doing the following which works fine when there is data to be passed in but when the string is blank it wont accept "" or even as a...
6
by: cjobes | last post by:
Hi all, I have a date field in a temp table as part of a DataSet. In some records the value is DBNull. how can I trap the error with if dtTemp("When") = ??? Then..... Thanks, Claus
4
by: Hon Yuen, Ng | last post by:
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...
10
by: Bob | last post by:
I'm sure there's a good reason, I just haven't been able to think of it - why didn't MS allow "DBNull" values to simply be a null (Nothing)? Bob
1
by: DUMMY Keyword in SQL Server 2000 | last post by:
I have a variable declared as integer which I populate based on the value on a grid. This value can be NULL. However, I get an exception when I try to save this value to the database even though...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.