[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

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

 table design
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

0
adamtrask
Asked:
adamtrask
1 Solution
 
code_meCommented:
please Check in the data type
If I am not wrong
in the date Field you have taken Time as Datatype
and in the time Field you have Taken date as Datatype

are you using SQl ,which version
check it and then reply
0
 
adamtraskAuthor Commented:
Thanks
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now