Solved

Convert char to smalldatetime

Posted on 2006-10-24
9
470 Views
Last Modified: 2008-02-01
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
Comment
Question by:running32
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17797845
DateTime.Parse(strtimein))
DateTime.Parse(strtimeout))

Bob
0
 

Author Comment

by:running32
ID: 17797877
I still get Syntax error converting character string to smalldatetime data type.   Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17797910
What does 'strtimein' and 'strtimeout' equal?

Bob
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:running32
ID: 17797956
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
 

Author Comment

by:running32
ID: 17797977
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17797997
Are you using U.S. dates, so that 10/24/2006 mean 24 Oct 06?

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17798030
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
 

Author Comment

by:running32
ID: 17798321
If I take it out I still get the error message.   I am using a US dates.  yes,
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17818549
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 78
Javascript to set controls visibility 5 34
encoding an href string 2 20
How to design the database - Asp.net mvc code first entity framework 1 37
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question