Failed to convert parameter message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dougancil
    Contributor
    • Apr 2010
    • 347

    #1

    Failed to convert parameter message

    I'm trying to pass values in a SessionState from one page to another and then post them to a SQL database. When I click on the submit button on my submit page, I get this error:



    Failed to convert parameter value from a String to a DateTime.



    Here is my submit page:

    Code:
    Imports System.Data.SqlClient
    Imports System.Data
    
    Partial Class Default2
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim opnumber As String = CType(Session.Item("opnumber"), String)
            Dim exceptiondate As DateTime = CType(Session.Item("exceptiondate"), DateTime)
            Dim stime As DateTime = CType(Session.Item("starttime"), DateTime)
            Dim etime As DateTime = CType(Session.Item("endtime"), DateTime)
            Dim hours As String = CType(Session.Item("hours"), String)
            Dim minutes As String = CType(Session.Item("minutes"), String)
            Dim exceptioncode As String = CType(Session.Item("exceptioncode"), String)
            Dim submittedby As String = CType(Session.Item("submittedby"), String)
            opnumLabel.Text = opnumber
            exceptdateLabel.Text = exceptiondate
            starttimeLabel.Text = stime
            endtimeLabel.Text = etime
            hourdurLabel.Text = hours
            mindurLabel.Text = minutes
            codeLabel.Text = exceptioncode
            approvedbyLabel.Text = submittedby
        End Sub
    
        Protected Sub sqlsubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles sqlsubmitButton.Click
            InsertRow()
        End Sub
        Public Sub InsertRow()
            Dim myConnectionString As String = "Initial Catalog=mdr;Data Source=xxxxxx;uid=xxxxx;password=xxxxxxxx;"
    
            Dim myConnection As New SqlConnection(myConnectionString)
            Dim commandText As String = _
            "insert into Exceptions (employeenumber, exceptiondate, starttime, endtime, duration, code, approvedby) " & _
            "values(@employeenumber, @exceptiondate, @starttime, @endtime, @duration, @code, @approved)"
    
            Using connection As New SqlConnection(myConnectionString)
                Dim command As New SqlCommand(commandText, connection)
    
                command.Parameters.Add("@employeenumber", SqlDbType.VarChar)
                command.Parameters("@employeenumber").Value = "opnumber"
    
                command.Parameters.Add("@exceptiondate", SqlDbType.DateTime)
                command.Parameters("@exceptiondate").Value = "exceptiondate"
    
                command.Parameters.Add("@starttime", SqlDbType.DateTime)
                command.Parameters("@starttime").Value = "starttime"
    
                command.Parameters.Add("@endtime", SqlDbType.DateTime)
                command.Parameters("@endtime").Value = "endtime"
    
                command.Parameters.Add("@duration", SqlDbType.VarChar)
                command.Parameters("@duration").Value = "hours, minutes"
    
                command.Parameters.Add("@code", SqlDbType.VarChar)
                command.Parameters("@code").Value = "exceptioncode"
    
                command.Parameters.Add("@approved", SqlDbType.VarChar)
                command.Parameters("@approved").Value = "submittedby"
    
                Try
    
                    connection.Open()
                    Dim rowsAffected As Integer = command.ExecuteNonQuery()
    
                Catch ex As Exception
                    myerrormessageLabel.Text = ex.Message
                End Try
                thankyouLabel.Text = "Your Data Has Been Submitted"
            End Using
    
        End Sub
    
        Protected Sub cancelinputButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cancelinputButton.Click
            Response.Redirect("Default.aspx")
        End Sub
    End Class
    Can anyone help me to figure out what I'm not seeing. I've verified that all of the input matches what I'm looking for on the SQL side.
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    which row of your code give this error.The exception message itself describes you are trying to convert a string which is not a valid date format.debug and see what value you are passing.

    Comment

    • dougancil
      Contributor
      • Apr 2010
      • 347

      #3
      I've removed all of the values that had "DateTime" in them so I know it's one of the datetime values that's causing this problem. There are only 3

      exceptiondate
      starttime
      endtime

      when I was debugging this last night, the values that I saw being passed were not null. I've tried inserting break points in a few places but can't see where it's failing.

      Comment

      • dougancil
        Contributor
        • Apr 2010
        • 347

        #4
        when I see the values of datetime, this is what I'm seeing

        (sample data)
        #11/15/2010#
        #11:00:00 AM#
        #12:00:00 PM#

        Comment

        • dougancil
          Contributor
          • Apr 2010
          • 347

          #5
          msmishra,

          Any ideas as to what I may be missing here? This is the last part of this project and I can't find what's causing this error.

          Comment

          Working...