• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

Convert char to smalldatetime

I am getting the error Syntax error converting character string to smalldatetime data type in asp.net when I try to upload a string to a smalldatetime field in sql.  My asp.net code is below and the format of the field is 1/3/2006 8:00:00 AM.  How can I get the time in without an error.

Thank you

strCmd = "insert into Employeehours (timein,timeout,rate,jobcode,company,costcode, employeeid, flag, inlocationid, outlocationid) values (@timein, @timeout,@rate, @jobcode,@company,@costcode, @employeeid, @flag, @Inlocationid, @Outlocationid) "
            objCmd = New SqlCommand(strCmd, objConn)
            objCmd.Parameters.Add("@timein", strtimein)
            objCmd.Parameters.Add("@timeout", strtimeout)
            objCmd.Parameters.Add("@employeeid", stremployeeid)
            objCmd.Parameters.Add("@flag", strflag)
            objCmd.Parameters.Add("@rate", strrate)
            objCmd.Parameters.Add("@jobcode", strjobcode)
            objCmd.Parameters.Add("@company", strcompany)
            objCmd.Parameters.Add("@costcode", strcostcode)
            objCmd.Parameters.Add("@Inlocationid", strlocation)
            objCmd.Parameters.Add("@Outlocationid", strlocation)
0
running32
Asked:
running32
  • 5
  • 4
1 Solution
 
Bob LearnedCommented:
DateTime.Parse(strtimein))
DateTime.Parse(strtimeout))

Bob
0
 
running32Author Commented:
I still get Syntax error converting character string to smalldatetime data type.   Thanks
0
 
Bob LearnedCommented:
What does 'strtimein' and 'strtimeout' equal?

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
running32Author Commented:
The error is actually in the code below.  I made the changes you sugested but it still give me the error.  Thanks

if Txttimein.enabled = true then
strtimein = DateTime.Parse(strtimein)
      objCmd.CommandText = "UPDATE Employeehours SET timein = '" & strtimein & "', Inlocationid = 6 where recordid = " & txtid.text
 objCmd.Connection = objConn
      objRdr = objCmd.ExecuteReader()

end if
0
 
running32Author Commented:
strdatein is the time they enter in text boxes.  The String looks like this and is made up from 3 text boxes 10/24/2006 6:59:00 AM.  It needs to be entered into the field formated this way.

Thanks
0
 
Bob LearnedCommented:
Are you using U.S. dates, so that 10/24/2006 mean 24 Oct 06?

Bob
0
 
Bob LearnedCommented:
This won't work:

   strtimein = DateTime.Parse(strtimein)

plus, it is not necessary for this code section, but was for the last one that you showed.

Bob
0
 
running32Author Commented:
If I take it out I still get the error message.   I am using a US dates.  yes,
0
 
Bob LearnedCommented:
Try this:

            Dim paramTimeIn As SqlParameter = objCmd.Parameters.Add("@timein", SqlDbType.SmallDateTime)
            paramTimeIn.Value = strtimein

            Dim paramTimeOut As SqlParameter = objCmd.Parameters.Add("@timeout", SqlDbType.SmallDateTime)
            paramTimeOut.Value = strtimeout


Bob
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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