?
Solved

Convert char to smalldatetime

Posted on 2006-10-24
9
Medium Priority
?
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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 2000 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

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!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

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