Solved

Convert char to smalldatetime

Posted on 2006-10-24
9
468 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now