Solved

Passing Time Value from ASP.Net VB page to SQL 2008 Time Parameter

Posted on 2009-04-06
1
721 Views
Last Modified: 2013-11-05
I have a text box on an ASP.net page that will be used to enter a time that will be stored in a SQL 2008 Time format data field.  Using the code below I get the message:  "Failed to convert parameter value from a String to a TimeSpan"

VB.Net does not seem to have a variable type to go with SQL 2008's new Time type.

Can anyone suggest how to get this to work... Change from string to another type on the vb.net side... change the Data.SqlDBType.Time to something else?

More info:  It works if I try to save any of the following:  12:45, 22:00, etc.  It does not work if I try to save 12:45PM.  I can make it work if I use 24 hour format, but I wonder if there's a way to save it using AM/PM?





Dim strHoseTestOrRepairTime As String = Nothing
 
            If Len(Trim(Me.textTestOrRepairTime.Text)) > 0 Then
                strHoseTestOrRepairTime = Trim(Me.textTestOrRepairTime.Text)
            Else
                strHoseTestOrRepairTime = "00:00"
            End If
 
            Dim myParameterTestOrRepairTime As New SqlParameter("@CHTRTime", Data.SqlDbType.Time)
            myParameterTestOrRepairTime.Direction = Data.ParameterDirection.Input
            myParameterTestOrRepairTime.Value = strHoseTestOrRepairTime
            myCommand.Parameters.Add(myParameterTestOrRepairTime)

Open in new window

0
Comment
Question by:TimSledge
[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
1 Comment
 
LVL 4

Accepted Solution

by:
TimSledge earned 0 total points
ID: 24081970
I found the answer myself.

You can capture, for example, 10:45PM as a string, the place it in the parameter using this format:  myParameterTestOrRepairTime.Value = DateTime.Parse("10:45PM").TimeOfDay


   If Len(Trim(Me.textTestOrRepairTime.Text)) > 0 Then
                strHoseTestOrRepairTime = Trim(Me.textTestOrRepairTime.Text)
            Else
                strHoseTestOrRepairTime = "00:00:00"
            End If
 
            Dim myParameterTestOrRepairTime As New SqlParameter("@CHTRTime", Data.SqlDbType.Time)
            myParameterTestOrRepairTime.Direction = Data.ParameterDirection.Input
            myParameterTestOrRepairTime.Value = DateTime.Parse(strHoseTestOrRepairTime).TimeOfDay
            myCommand.Parameters.Add(myParameterTestOrRepairTime)

Open in new window

0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

737 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