Solved

Convert char to smalldatetime

Posted on 2006-10-24
9
472 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Technology Partners: 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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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