Thanks Jason,
 
I found the problem, I have for some reason added 

Dim DBNull As Object

Not sure why, may have something to do with this week I was trying to work
this out. Maybe I need to drink a bit more, it may help with my coding.
 
My code now runs correctly. 

  _____  

From: [email protected] [mailto:[email protected]]
On Behalf Of Jason Keats
Sent: Saturday, 9 April 2011 11:35 AM
To: ozDotNet
Subject: Re: Writing NULL value to MS SQL Database


I just tried your example. It had no problem inserting a Null using
DBNull.Value.

It must be something else causing your problem.

Hopefully, I got it right this time - even with a hangover. ;-)



On Fri, Apr 8, 2011 at 9:36 PM, Darron and Kerrie
<[email protected]> wrote:


I have just started programming in VB.net. 
 
I am recoding an old VB6.0 program to VB.net, and having a problem with
setting a database field back to null. 
 
The VB6.0 code was:
 
Set rs = OpenTable("select * from tblunits where unit = " & "'" & temp &
"'")
rs!status = "Available"
rs!eventnum = Null
rs.Update
 
I have tried a number of different way in VB.net but always get errors. Here
is my current code.
 
Dim

sqlda As New SqlDataAdapter 



Dim SQLcmd As New SqlCommand 



Dim TableFlag As DataTable 



Dim drunitstatus As DataRow 





With SQLcmd 

.Connection = conn

.CommandType = 

CommandType.Text 

.CommandText = (

"select * from tblunits where unit = '" & UnitName & "'") 



End With 

sqlda = 

New SqlDataAdapter(SQLcmd) 

TableFlag = 

New DataTable 

sqlda.Fill(TableFlag)

drunitstatus = TableFlag.Rows(0)

drunitstatus("status") = "Available"



drunitstatus("eventnum") = DBNull.value 'This appear to be the problem line
"eventnum" is int32 and allow nulls 

Dim

sCB As SqlCommandBuilder = New SqlCommandBuilder(sqlda) 

sqlda.Update(TableFlag)

 

The Current error is "NullReferenceException was Caught"

Can any one advise what I am doing wrong?

Darron

 



 


Reply via email to