Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBNull - how to insert a NULL into DB?

Status
Not open for further replies.

axa4

Technical User
Dec 7, 2002
6
0
0
US
Hi,
I'm using simple INSERT statement (not stored proc.)

Here is what I have:
Dim varDate as Object

IF txtDate.text = String.Empty then
varDate = System.DBNull.Value
ELSE
varDate = txtDate.text
END IF

It doesn't work - I'm getting that dafault thing 1/1/1900 each time I insert an empty date.

Thank you in advance.
 
varDate = "NULL"

I think that's what you're after... The following statement is valid SQL:

INSERT INTO tblDates (theDate) VALUES (NULL);

penny1.gif
penny1.gif
 
It could be your test of txtDate.Text.

Have you stepped through your code to make sure that the varDate is actually getting set to dbNull.Value? If it isn't, that would explain why you're getting the default date, and should try If txtDate.Text <> &quot;&quot; Then

D'Arcy
 
I have found something on but I'm not sure how to use it - do I need a stored procedure or not?

Here is what I have now:
(it gives an error: &quot;Cast from type 'DBNull' to 'String' in not valid in String.Format(&quot;INSERT INTO Employees(FirstName, ...)
************************************
Private Sub InsertEmp()
Dim strSQL As String
Dim strConn As String
Dim sEvalLast As Object

strConn = Session(&quot;ConnectString&quot;).ToString

If txtEvalLast.Text = String.Empty Then
sEvalLast = Convert.DBNull ' or System.DBNull.Value

Else
sEvalLast = DataHandler.QuoteString(txtEvalLast.Text)
End If

strSQL = String.Format(&quot;INSERT INTO Employees(FirstName, LastName, &quot; & _
&quot;SupervisorID, DateLastEval) &quot; & _
&quot;VALUES({0}, {1}, {2}, {3}, {4}) &quot;, _
DataHandler.QuoteString(txtFirstName.Text), _
DataHandler.QuoteString(txtLastName.Text), _
ddlSupervisor.SelectedItem.Value, _
DataHandler.QuoteString(sEvalLast))

DataHandler.ExecuteSQL(strSQL, strConn)
Response.Redirect(&quot;Employees.aspx&quot;)
End Sub
==================================================

Here is the code.
I don't know how to use it in my case

**************************************
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandText = &quot;INSERT INTO myTable (Name, RegisteredDate, CancelDate) &quot; & _
&quot;VALUES (@Name, @RegisteredDate, @CancelDate)&quot;
cmd.Parameters.Add(&quot;@Name&quot;, &quot;Doug Seven&quot;)
cmd.Parameters.Add(&quot;@RegisteredDate&quot;, DateTime.Today)
'Use System.DBNull.Value to leave the field uninitialized
cmd.Parameters.Add(&quot;@CancelDate&quot;, System.DBNull.Value)

'checking for null
If user.RegisteredDate = Nothing Then
cmd.Parameters(&quot;@RegisteredDate&quot;).Value = System.DBNull.Value
Else
cmd.Parameters(&quot;@RegisteredDate&quot;).Value = user.RegisteredDate
End If
*********************************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top