473,666 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1104
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(mess age) 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
1189
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 Enterprise Manager. field How do I prevent this? However the 'RecCreated' doess not permit nulls. CREATE TABLE . ( IDENTITY (1000, 1) NOT NULL , (50) NOT NULL ,
3
1728
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; DataRow dr = ...; dr = foo; However, this does not work if the DataType value is set to int. In that
14
1793
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 makes up a saved query called Query1
5
412
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 "good" string data that I need to parse. This is evidenced by the fact that string.length returns 36 when the actual string data I need to work with is usually much less. I tried using String.Trim(null) but this doesn't achieve the desired result.
14
1903
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 are - it is a real problem if you have Nullable fields. You can't always use a 0 or -1 to tell whether a field is null and dates are another problem. I don't want to use some obscure date in the future for something like "EndDate" or...
8
2320
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 hande NULL's being pulled from the DB now? When I try to access a page and if the field is NULL I get an error: i do i fix this? I'm using VB.NET for coding Cast from type 'DBNull' to type 'String' is not valid.
1
1288
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 field is no longer NULL. I have written a script to test for empty textboxes and insert DB.NULL which is fine but when I want to load the data back into the form
1
1366
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 string. The problem is the packet string apparently has nulls or something in the unused space up to 1000. and of course when I append a string AFTER the packet string it doesn't display.How can I stick a b yte array into a string removing the...
13
3322
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 these arrays but can't use strlen because it just stops counting at the first null it finds. so how to do it? Angus
3
13871
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, fromEncrypt.Length); //At this point in the debugger, I can see there are "trailing" NULLs '\0'
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.