Nulls in String data type

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.
 
G

Guest

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
 
G

Guest

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)
 
A

Armin Zingler

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top