PC Review


Reply
Thread Tools Rate Thread

Insert Null Value to SQL Server

 
 
Sam
Guest
Posts: n/a
 
      22nd May 2004
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,


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd May 2004
Sam <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      22nd May 2004
Sam wrote:

> 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

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      24th May 2004
Thanks a lot, it works !

"Sam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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,
>
>



 
Reply With Quote
 
Manu
Guest
Posts: n/a
 
      27th May 2004
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


 
Reply With Quote
 
P@rick
Guest
Posts: n/a
 
      27th May 2004
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
....


"Manu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert query with null column and null value =?Utf-8?B?RXVnZW5l?= Microsoft Access Queries 10 5th Oct 2007 04:41 AM
Insert NULL into SQL Server Image column Alasdair Thomson Microsoft ADO .NET 1 15th Jan 2005 08:07 PM
How to INSERT pure <NULL> Into SmallDataTime Field (MS SQL Server) esoroka@gmail.com Microsoft ADO .NET 3 29th Dec 2004 09:50 PM
Insert null date from XML data to SQL Server Roman Microsoft VB .NET 0 25th Oct 2004 03:39 PM
Insert NULL value in SQL Server Dinko Deranja Microsoft ADO .NET 2 17th Sep 2004 01:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:43 PM.