Nulls in String data type

  • Thread starter Thread starter Steve Holland
  • Start date Start date
S

Steve Holland

Can a variable declared as string (Dim strMessage as
String) be set to a Null value??? If so, how??? I am
using SQL Server as a backend database and want to set
values not populated on a VB.Net form to Null in the
database. I am using an Insert statement along with the
SQLCommand object to do this. Thanks.
 
Try this

dim sInsertValue as Strin
if strMessage = "" the
sInsertValue = "null
els
sInsertValue = "'" & strMessage & "'
end i

The SQL Statement should be: INSERT into table_name(message) values (sInsertValue

Hope this helps
 
I think that code would insert the word "null" into the
column value instead of Null (DBNull, which is what I
want) meaning the column has never received a value.
Thanks.
-----Original Message-----
Try this:

dim sInsertValue as String
if strMessage = "" then
sInsertValue = "null"
else
sInsertValue = "'" & strMessage & "'"
end if

The SQL Statement should be: INSERT into table_name
(message) values (sInsertValue)
 
Steve Holland said:
Can a variable declared as string (Dim strMessage as
String) be set to a Null value??? If so, how??? I am
using SQL Server as a backend database and want to set
values not populated on a VB.Net form to Null in the
database. I am using an Insert statement along with the
SQLCommand object to do this. Thanks.

If you use parameters with the SQLCommand, set the parameter value to
DBNull.Value. If you are building the SQL string on your own, use the String
"Null" (e.g. "...set field1 = null"). Using parameters should be the
prefered way.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Back
Top