I really need your help on this matter. I have search many similar topics about this but cannot find the right one for my program. Ill try to simplify my question and exclude other details which is not related on my concern.
My vb program has a datetimepicker named dtpDate, 2 buttons named btnRecord_Save and btnClear_Date.
My sql has a table named Voucher with a table named Voucher_Date (Datatype = Date)
When my dtpdate has a date value, i have no issue as it works perfectly fine, but if i clear the date on dtpdate by pressing btnClear_date, it gives me an error (as mentioned on above title). All i wanted is if dtpDate is blank, it will store 'NULL' value on my sql (Column Name = Voucher_date).
Here is my code for your reference, i have edited and excluded unnecessary information.
CLASS CODE (SQLControl)
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class SQLControl
Public SQLCon As New SqlConnection With {.ConnectionString i got this!!!}
Public SQLDA As SqlDataAdapter
Public SQLDS As DataSet
Public Params As New List(Of SqlParameter)
Public RecordCount As Integer
Public Exception As String
Private SQLCmd As SqlCommand
Public Sub AddToVoucher(Voucher_Date As Date)
Try
Dim strInsert As String = "INSERT INTO Voucher (Voucher_Date) " & _
"VALUES (" & _
"'" & Voucher_Date & "') "
SQLCon.Open()
SQLCmd = New SqlCommand(strInsert, SQLCon)
SQLCmd.ExecuteNonQuery()
SQLCon.Close()
MsgBox("Successfully Added Into Records!")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Public Class FormMain
Dim sql As New SQLControl
Private Sub dtpDate_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtpDate.ValueChanged
dtpDate.CustomFormat = "dd/MM/yyyy"
End Sub
Private Sub btnClearDate_Click(sender As System.Object, e As System.EventArgs) Handles btnClearDate.Click
dtpDate.CustomFormat = " "
dtpDate.Format = DateTimePickerFormat.Custom
End Sub
Private Sub btnRecord_Save_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord_Save.Click
sql.AddToVoucher(dtpDate.Text)
End Sub
End Class