Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Null as value

Author  Topic 

TZ123
Starting Member

1 Post

Posted - 2004-10-18 : 10:13:59
Hi,

How to insert Null as value in fields like Number and Date
the users want sometimes to leave dates or number fields blank
so they erase the textbox value but then they get "operation was canceled " error message
to fix this I use the Enterprise maneger return all rows
and insert null by 'Ctrl' & '0' buttons
but I want to let the users insert null by themselves

I try the following code but don't work

if Text1.text="" or isnull(Text1.text) then _
adodc1.recordset("Numfield").value =Null



Pls. help
tnx
TZ

brendalisalowe
Constraint Violating Yak Guru

269 Posts

Posted - 2004-10-18 : 12:35:09
I have done something like this:

Dim Update_docs As String
Dim SqlConn As SqlConnection
Dim cmdSqlCommand As SqlCommand
Update_docs = "UPDATE tblCapRec SET " & _
"AppSent2 = @AppSent2 " & _
"WHERE CaseNumber ='" & g_CaseNum & "' " & _
"AND PartNumber = " & g_PartNum & ""
SqlConn = New SqlConnection(strConnectionString)
cmdSqlCommand = New SqlCommand(Update_docs, SqlConn)
Call Me.AddParameters(cmdSqlCommand)
Call Me.AssignParameterValues(cmdSqlCommand)
Try
cmdSqlCommand.Connection.Open()
cmdSqlCommand.ExecuteScalar()
Catch exp As Exception
MessageBox.Show(exp.Message)
Exit Sub
Finally
If SqlConn.State = ConnectionState.Open Then
SqlConn.Close()
End If
End Try


Private Sub AddParameters(ByRef cmd As SqlCommand)
With cmd.Parameters
.Add(New SqlParameter("@AppSent2", SqlDbType.DateTime))
End With
End Sub

Private Sub AssignParameterValues(ByRef cmd As SqlCommand)
With cmd.Parameters
If Me.dtpAppSent2.Format <> DateTimePickerFormat.Custom Then
.Item("@AppSent2").Value = Me.dtpAppSent2.Value
Else
.Item("@AppSent2").Value = DBNull.Value
End If
End Sub


#Region "Make Dates Blank"
Private Sub lblAppSent2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAppSent2.Click
Me.dtpAppSent2.Format = DateTimePickerFormat.Custom
Me.dtpAppSent2.CustomFormat = " "
Dim Update_docs As String
Dim SqlConn As SqlConnection
Dim cmdSqlCommand As SqlCommand
Update_docs = "UPDATE tblCapRec SET " & _
"AppSent2 = @AppSent2 " & _
"WHERE CaseNumber ='" & g_CaseNum & "' " & _
"AND PartNumber = " & g_PartNum & ""
SqlConn = New SqlConnection(strConnectionString)
cmdSqlCommand = New SqlCommand(Update_docs, SqlConn)
Call Me.AddParameters(cmdSqlCommand)
Call Me.AssignParameterValues(cmdSqlCommand)
Try
cmdSqlCommand.Connection.Open()
cmdSqlCommand.ExecuteScalar()
Catch exp As Exception
MessageBox.Show(exp.Message)
Exit Sub
Finally
If SqlConn.State = ConnectionState.Open Then
SqlConn.Close()
End If
End Try
#End Region


Does that help?

Brenda

If it weren't for you guys, where would I be?
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-10-18 : 17:35:12
quote:
Originally posted by TZ123

Hi,

How to insert Null as value in fields like Number and Date
the users want sometimes to leave dates or number fields blank
so they erase the textbox value but then they get "operation was canceled " error message
to fix this I use the Enterprise maneger return all rows
and insert null by 'Ctrl' & '0' buttons
but I want to let the users insert null by themselves

I try the following code but don't work

if Text1.text="" or isnull(Text1.text) then _
adodc1.recordset("Numfield").value =Null



Pls. help
tnx
TZ



First the field must allow null values then you can use
if not(trim(Text1.text)="") then _
adodc1.recordset("Numfield").value =Text1.text
end if


if you use an SQL insert statement you may specify Null for the empty fields or name all the fields
that have values and then the values in the same order. SQL will asign Null to the empty fields
onless the default value is other than Null

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*

A candle loses nothing by lighting another candle
Go to Top of Page
   

- Advertisement -