473,385 Members | 1,673 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.

Nulls in String data type

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.
Nov 20 '05 #1
3 1095
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
Nov 20 '05 #2
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)
Hope this helps !
.

Nov 20 '05 #3
"Steve Holland" <St***********@leg.state.nh.us> schrieb
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

Nov 20 '05 #4

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

Similar topics

7
by: grist2mill | last post by:
I have a simple table, for some reason, certain columns seem to accept Nulls even though they shouldn't, for example the I can set the 'Name' field to Null using my web application or directly in...
3
by: Thomas Coleman | last post by:
I have been playing around with 2.0 and I'm trying clarify a few things about generics and data access. I was hoping to be able to do something like this with nullable types: int? foo = null; ...
14
by: Salad | last post by:
A97. Situation: I have 3 tables with a text field in each and a date field in the first 2 tables: Table1 Text1, Date1 Table2 Text2, Date2 Table3 Text3 (no date field) The following...
5
by: Drew | last post by:
I have been using System.Text.Encoding.Unicode.GetString(byte,0,72) to converter a byte array from the registry to a string. This works, but I end up with a bunch of null characters after the...
14
by: tshad | last post by:
I have people telling me that I should set up objects for my tables, but I am finding the Null problem makes that difficult. It isn't a big problem if you are not updating the table, but if you...
8
by: Mike | last post by:
The current databas structure that i'm working with allowed NULL's an now I'm converting the app to .NET and it will not allow NULLs in the fields when populated. So my question is, how can i...
1
by: Angela | last post by:
Hi I am building a very simple data entry system with many forms and input textboxes. When I leave any part of the form empty (it allows nulls) it seems to touch the field in the DB and the...
1
by: buc | last post by:
I have created a byte array (1000 in size) to recv a network packet. I recv the packet, which varies in size, and need to put the packet in a string; so I can append another string after the packet...
13
by: Angus | last post by:
Hello I have a stream of bytes - unsigned char*. But the 'string' may contain embedded nulls. So not like a traditional c string terminated with a null. I need to calculate the length of...
3
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hello, I've eliminated the bulk of code, this should be sufficient: byte fromEncrypt; string sDecryptedString; //Read the data out of the crypto stream. csDecrypt.Read(fromEncrypt, 0,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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:
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.