Link to home
Start Free TrialLog in
Avatar of adamtrask
adamtrask

asked on

Saving data into SqlServer

Hello experts,

Will you please help me find why I can't save the date and Time in SqlServer database.

I am attaching a snap shot of the table design where the data should be saved. I am also attaching the code in VB.net which saves the data into the table.

The problem is simple, the record is saved except for the dData and tTime columns. I get 00:00:00 in the dDate column and 1900-01-01 in the tTime column

The date and time are initially entered into two separate text Boxes  "txtDate" and "txtTime":
The code for entering the date is :

dim currentDate as date = Now
txtDate.Text = currentDate.ToShortDateString

as for entering time the code is:

dim currentTime as date =Now
txtTime.text = currentTime.ToShortTimeString

Below is the code for inserting the data into the table

Thanks

 User generated image
Dim con As SqlConnection
        Dim comm As SqlCommand

        con = New SqlConnection("Server=Adam\; Database=HelpDesk;Integrated Security=True")
        ' create the INSERT query 
        Dim strQuery As String = "INSERT INTO Main (dDate,tTime,nName,sSubject,mMonth,tType,Remarks) VALUES (@dDate,@tTime,@nName,@sSubject,@mMonth,@tType,@remarks);"
        comm = New SqlCommand(strQuery, con)
        comm.Parameters.AddWithValue("@dDate", System.Data.SqlDbType.Date)
        comm.Parameters("@dDate").Value = Me.txtDate.Text
        comm.Parameters.AddWithValue("@tTime", System.Data.SqlDbType.Time)
        comm.Parameters("@tTime").Value = Me.txtTime.Text
        comm.Parameters.AddWithValue("@nName", System.Data.SqlDbType.NVarChar)
        comm.Parameters("@nName").Value = txtName.Text
        comm.Parameters.AddWithValue("@sSubject", System.Data.SqlDbType.NVarChar)
        comm.Parameters("@sSubject").Value = txtSubject.Text
        comm.Parameters.AddWithValue("@mMonth", System.Data.SqlDbType.NVarChar)
        comm.Parameters("@mMonth").Value = txtMonth.Text
        comm.Parameters.AddWithValue("@tType", System.Data.SqlDbType.NVarChar)
        comm.Parameters("@tType").Value = txtType.Text
        comm.Parameters.AddWithValue("@Remarks", System.Data.SqlDbType.NVarChar)
        comm.Parameters("@Remarks").Value = txtRemarks.Text
        Try
            con.Open()
            ' Execute the command
            comm.ExecuteNonQuery()
        Finally
            con.Close()
        End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of code_me
code_me

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adamtrask
adamtrask

ASKER

Thanks