Insert Null Value to SQL Server

S

Sam

Hi,
I have problem inserting null value through SQLCommand in VB.NET to SQL
Server.

dim cm as new sqlcommand
cm.commandtext = "insert into table1 (field1, field2, ....) values(var1,
var2,....)"

How to assign var2 <== NULL Value, so in SQL Server will appear as null
value ?

thanks,
 
J

Jon Skeet [C# MVP]

Sam said:
I have problem inserting null value through SQLCommand in VB.NET to SQL
Server.

dim cm as new sqlcommand
cm.commandtext = "insert into table1 (field1, field2, ....) values(var1,
var2,....)"

How to assign var2 <== NULL Value, so in SQL Server will appear as null
value ?

Use parameters and set the value to DBNull.Value.

See http://www.pobox.com/~skeet/csharp/faq/#db.parameters for more
information.
 
F

Frans Bouma [C# MVP]

Sam said:
Hi,
I have problem inserting null value through SQLCommand in VB.NET to SQL
Server.

dim cm as new sqlcommand
cm.commandtext = "insert into table1 (field1, field2, ....) values(var1,
var2,....)"

How to assign var2 <== NULL Value, so in SQL Server will appear as null
value ?

Simply do not specify the field:
cm.commandtext = "insert into table1 (field1, field3, ....) values(var1,
var3,....)"

Or, as Jon said, use parameters and set the value to DBNull.Value or to
Nothing (VB.NET) or null (C#)

FB
 
M

Manu

Is there any way i can assign a null value to a Feild in a dataset where the
allow nulls property of that feild is set to false


Manu
 
P

P@rick

hmmm .... your question seems to be a bit curious ...
if you have to enter a null value in a field, which does not allow to be
null ... you have to think about your design ....

Regards,
P@rick

PS: I think (hope) there is no way to do this action ... but I'm not sure
....
 

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