Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code i used to save id and date.
If I did not give the value for datetextbox it takes as the default datetime of a system.
I want that If i did not give any value for datetextbox it stores a null value in sql. Please give the codings

C#
SaveID(IDTextbox.text,DateTextbox.text)
Private Function SaveID(ByVal ID As String,ByVal Date As DateTime)
SaveID = False
     Try
       Me.MySqlConnect(My.Settings.AAConnectionString)
       Dim MySqlCmd As SqlClient.SqlCommand
       MySqlCmd = New SqlCommand("INSERT INTO [AA].[dbo].[tbl_ID]([ID],[Date]) VALUES (@ID,,@Date)", Me.Connection)
       MySqlCmd.Parameters.Add(New SqlParameter("@ID", SqlDbType.NVarChar, 10))
       MySqlCmd.Parameters("@ID").Value = ID
       MySqlCmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.DateTime))
       MySqlCmd.Parameters("@Date").Value = Convert.ToDateTime(Date)
       MySqlCmd.CommandType = CommandType.Text
       MySqlCmd.ExecuteNonQuery()
       Me.Connection.Close()
       SaveID = True
     Catch e As Exception
       MsgBox(e.Message)
     End Try
END Function
Posted
Updated 20-Jan-15 0:41am
v3

Why don't you use a Nullable (Of DateTime)[^]?
 
Share this answer
 
USE THIS CODE IT DOES STORE NULL IN SQL


SaveID(IDTextbox.text,DateTextbox.text)
Private Function SaveID(ByVal ID As String,ByVal Date As DateTime)
SaveID = False
Try
Me.MySqlConnect(My.Settings.AAConnectionString)
Dim MySqlCmd As SqlClient.SqlCommand
MySqlCmd = New SqlCommand("INSERT INTO [AA].[dbo].[tbl_ID]([ID],[Date]) VALUES (@ID,,@Date)", Me.Connection)
MySqlCmd.Parameters.Add(New SqlParameter("@ID", SqlDbType.NVarChar, 10))
MySqlCmd.Parameters("@ID").Value = ID
If Date<> Nothing Then
MySqlCmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.DateTime))
MySqlCmd.Parameters("@Date").Value = Convert.ToDateTime(Date)
Else
Dim getDate As SqlDateTime
getDate = SqlDateTime.Null
MySqlCmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.NVarChar, 100))
MySqlCmd.Parameters("@Date").Value = getDate
end if
MySqlCmd.CommandType = CommandType.Text
MySqlCmd.ExecuteNonQuery()
Me.Connection.Close()
SaveID = True
Catch e As Exception
MsgBox(e.Message)
End Try
END Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900