Hello,
Could someone help me with this question that I need to complete within 4 days? I have a problem with the insert statment on the code below. This code is working fine by inserting the ststement, but the problem is that the DATE is not being inserted into the SQL DB. The default date is being inserted as 1900-01-01 00:00:00.000. I really want today's date to be inserted into the SQL DB when the other data are being inserted. FYI - At the SQL level the column's data type is Date. I have tried the other way to get the Date column to be updated automatically with the Getdate() function but it's not working. Please help solve what is wrong with my code below. NB: class DALCallRegistry is a class the frmCallTally class uses it.

Imports System.Data.SqlClient

Public Class DALCallRegistry
    Inherits DALSqlDb
    Dim strSQL As String

    Sub InsertCallData(ByVal calldate As String, ByVal calltime As String, ByVal servicedeskanalyst As String, ByVal typeofcall As String, ByVal comment As String)
        Try

            'calldate = FormatDateTime(calldate, DateFormat.ShortDate)
            'calltime = FormatDateTime(calltime, DateFormat.ShortTime)
            strSQL = "INSERT INTO tblCallTally " & _
                    "(Call_Date, Call_Time, Service_Desk_Analyst, CallType, Comment, StaffNumber) " & _
                    "VALUES(" & calldate & ", '" & calltime & "', '" & servicedeskanalyst & "', '" & typeofcall & "', '" & comment & "', '" & gstrStaffNo & "')"
            ExecuteSQL(strSQL)
            'cmd.ExecuteNonQuery()
            'MessageBox.Show("Records saved!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

    Sub LoadCallType()
        Try
            strSQL = "SELECT * FROM tblCallType"
            PopulateDataset(strSQL, "tblCallType")
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

End Class


Imports System.Data
Imports System.Data.SqlClient

Public Class frmCallTally
    Dim objCall As New DALCallRegistry
    'Dim callDate As String

    Private Sub btnCaptureDateTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCaptureDateTime.Click
        'To stop the time from working
        'callDate = lblDate.Text
        'String.Format("dd/mm/yyyy").ToString()
        Timer1.Enabled = False
        grpCall.Enabled = True
        btnSave.Enabled = True
        cmbTypeOfCall.Focus()
        'After capturing change the back color of the button toi white as normal.
        btnCaptureDateTime.BackColor = Color.Red
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblDate.Text = DateAndTime.Now.ToLongDateString()
        lblDisplayTime.Text = DateAndTime.Now.ToLongTimeString()
    End Sub

    Private Sub btnMainForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainForm.Click
        Dim frmChr As New MDIParentForm
        frmMainMenu.MdiParent = Me.MdiParent
        frmMainMenu.Show()
        Me.Close()
    End Sub

    Private Sub btnChangePassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangePassword.Click
        Dim frmChr As New MDIParentForm
        frmChangePassword.MdiParent = Me.MdiParent
        frmChangePassword.Show()
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'To stop the time from working
        Timer1.Enabled = True
        grpCall.Enabled = False
        'After capturing change the back color of the button toi white as normal.
        btnCaptureDateTime.BackColor = Color.Lime
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Try

            lblDate.Text = FormatDateTime(lblDate.Text, DateFormat.ShortDate)
            lblDisplayTime.Text = FormatDateTime(lblDisplayTime.Text, DateFormat.ShortTime)

            objCall.InsertCallData(lblDate.Text, lblDisplayTime.Text, gstrUserID, cmbTypeOfCall.Text, txtComments.Text)
            MessageBox.Show("Records saved!", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
            grpCall.Enabled = False
            txtComments.Text = ""
            btnSave.Enabled = False

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        
    End Sub

    Private Sub frmCallTally_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Disables the group box
        grpCall.Enabled = False
        btnSave.Enabled = False
        Try
            'Method in BOLCall Class
            objCall.LoadCallType()

            With Me.cmbTypeOfCall
                .DataSource = objCall.dss.Tables("tblCallType")
                .DisplayMember = "CallType"
                .SelectedIndex = 0
            End With

        Catch ex As Exception
            Throw New Exception(ex.Message)
            'MessageBox.Show("Connection to database failed! Please contact your system administrator")
        End Try
    End Sub
End Class

What is the field in database? and Call_Date is datetime datatye? it should be datetime. and instead of passing it as parameter u can use system.datetime.now.

You are not showing what you are using to populate calldate, so I'm skipping it. Instead go with getdate(), but use it with

convert(char(10),getdate(), )

and add the appropriate date style. You can look it up here http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx

Thanks Pgmer and adam_K for your replies. I have managed to solve this.

Good..Then please mark the thread as solved.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.