473,406 Members | 2,549 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,406 software developers and data experts.

How to assign DBNull to a SQL field?

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, dateWitness2Date.Value, DBNull.Value), DateTime), & ...

It says "Cast from DBNull to Data is not valid"

If I relpace DBNull.Value with Nothing, then it seem to work but assigns MinDate instead of Null to the value.

Can you please give me some advise how to do this? Thank you.
Nov 20 '05 #1
4 14891
Omit the parameter completely:
CType(IIf(dateWitness2Date.Checked, dateWitness2Date.Value), DateTime), &
....

This assumes the Database field has AllowNull= True.
--
Joe Fallon


"Dursun" <Dursun @discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
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, dateWitness2Date.Value, DBNull.Value), DateTime), & ...
It says "Cast from DBNull to Data is not valid"

If I relpace DBNull.Value with Nothing, then it seem to work but assigns MinDate instead of Null to the value.
Can you please give me some advise how to do this? Thank you.

Nov 20 '05 #2
> > CType(IIf(dateWitness2Date.Checked, dateWitness2Date.Value,
DBNull.Value),
DateTime), & ...
Why are you doing CType(<blah>, DateTime) ?

dateWintness2Date.Value is already of that type, no?

and you can't cast DBNull.Value to DateTime and you wouldn't even want to

Just use:
cmd.parameters.add("@myDate", sqldbtype.datetime).value =
IIf(dateWitness2Date.Checked, dateWitness2Date.Value, DBNull.Value)

Although I prefer to avoid the IIf function and do:

if dateintness2Date.Checked then
cmd.parameters.add("@myDate", sqldbtype.datetime).value =
dateWitness2Date.Value
else
cmd.parameters.add("@myDate", sqldbtype.datetime).value = DBNull.Value
end if

You are using parameters???

Greg
"Dursun" <Du****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Good idea but it reqires me to have an object for the FALSE part of the IIF statement. I even tried to put the "," and nothing else but that did not
work either. Any other ideas? Please...
"Joe Fallon" wrote:
Omit the parameter completely:
CType(IIf(dateWitness2Date.Checked, dateWitness2Date.Value), DateTime),

& ....

This assumes the Database field has AllowNull= True.
--
Joe Fallon


"Dursun" <Dursun @discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
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, dateWitness2Date.Value,
DBNull.Value), DateTime), & ...

It says "Cast from DBNull to Data is not valid"

If I relpace DBNull.Value with Nothing, then it seem to work but
assigns MinDate instead of Null to the value.

Can you please give me some advise how to do this? Thank you.


Nov 20 '05 #3
I use the If..Else method also, but generally employ stored procedures for
all updates and inserts. For optional values I specify a parameter default
in the stored procedure of null... so I only pass the parameters to the sp
if my input has text/is checked, etc:

If dateintness2Date.Checked Then
cmd.parameters.add("@myDate", sqldbtype.datetime).value =
dateWitness2Date.Value
End If


"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uv**************@TK2MSFTNGP11.phx.gbl...
CType(IIf(dateWitness2Date.Checked, dateWitness2Date.Value, DBNull.Value), DateTime), & ...
Why are you doing CType(<blah>, DateTime) ?

dateWintness2Date.Value is already of that type, no?

and you can't cast DBNull.Value to DateTime and you wouldn't even want to

Just use:
cmd.parameters.add("@myDate", sqldbtype.datetime).value =
IIf(dateWitness2Date.Checked, dateWitness2Date.Value, DBNull.Value)

Although I prefer to avoid the IIf function and do:

if dateintness2Date.Checked then
cmd.parameters.add("@myDate", sqldbtype.datetime).value =
dateWitness2Date.Value
else
cmd.parameters.add("@myDate", sqldbtype.datetime).value = DBNull.Value
end if

You are using parameters???

Greg
"Dursun" <Du****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Good idea but it reqires me to have an object for the FALSE part of the IIF statement. I even tried to put the "," and nothing else but that did

not work either. Any other ideas? Please...

"Joe Fallon" wrote:
Omit the parameter completely:
CType(IIf(dateWitness2Date.Checked, dateWitness2Date.Value),
DateTime), & ....

This assumes the Database field has AllowNull= True.
--
Joe Fallon


"Dursun" <Dursun @discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
> 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, dateWitness2Date.Value, DBNull.Value), DateTime), & ...
>
> It says "Cast from DBNull to Data is not valid"
>
> If I relpace DBNull.Value with Nothing, then it seem to work but assigns MinDate instead of Null to the value.
>
> Can you please give me some advise how to do this? Thank you.


Nov 20 '05 #4
Hi Dursun,

I would not use the IIF forever however for sure not in this case.

Testing to dbnull.value is testing to an object what means

If dr(0)(0) Is DbNull.value

I hope this helps?

Cor
Nov 20 '05 #5

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

Similar topics

6
by: Ravikanth[MVP] | last post by:
Hi Alternative is check before assigning as TextBox value. TextBox1.Text =myRow==DBNull.Value)?"": (string)myRow HTH Ravikanth
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...
2
by: Filipe Cristóvão | last post by:
Hi, I know that this is a newbie's question, but I need to know how i do to know if a field in a table is DBNull. If I try to write (with response.write) the field, it gives me an error: "Cast...
1
by: Ed Chiu | last post by:
Hi, Is there a way to assign a DBNull value to an optional parameter of a function? I tried: Public Function AddUpdateCases( _ ByVal CrisCaseID As Integer, _ Optional ByVal APN As DateTime...
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,...
2
by: Julian | last post by:
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,...
3
by: Liam Mac | last post by:
Hi All, Can anyone direct me or provide advice on how I can assign a null value to a date variable in vb.net. Basically what I'm doing is that I'm looping through a recordset where I have three...
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...
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
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
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
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.