locked
Inserting New Record Fails from Date Format RRS feed

  • Question

  • User-1812511197 posted

    Here is the exception error I am seeing.   I have narrowed it down to a Date format problem.

                               Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.

    The To.Strings cammand extract the date the way I need.

            Try
                Dim Cnn As New OleDb.OleDbConnection("File Name=E:\Web\rhi\UDL\clockno.udl")
                Dim Command As New OleDb.OleDbCommand("INSERT INTO tbStartTime(dbClockNo, dbName, dbDate, dbDateTime) VALUES(@dbClockNo, @dbName, @dbDate, @dbDateTime)", Cnn)
    
                Dim varStartDate As Date
                Dim varStartTime As Date
    
                varStartDate = txtStartTime.Text.ToString("d")
                varStartTime = txtStartTime.Text.ToString("g")
    
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbClockNo", OleDb.OleDbType.VarChar, 10))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbName", OleDb.OleDbType.VarChar, 25))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbDate", OleDb.OleDbType.Date))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbDateTime", OleDb.OleDbType.Date))
    
                Command.Parameters("@dbClockNo").Value = Me.gvName.SelectedDataKey.Item(0)
                Command.Parameters("@dbName").Value = Me.gvName.SelectedDataKey.Item(1)
                Command.Parameters("@dbDate").Value = varStartDate
                Command.Parameters("@dbDateTime").Value = varStartTime
    
                Cnn.Open()
                Command.ExecuteNonQuery()
                Cnn.Close()
    
                lblError.Text = ""
            Catch ex As Exception
                'lblError.Text = "Error: Start Time Was Not Inserted"
                lblError.Text = ex.Message
            End Try
    



    Monday, April 9, 2012 1:56 PM

Answers

  • User-1812511197 posted

    I figured it out.  I should have seen this.

    I need to format the variable not the text box.

    Here is the corrected code.

            Dim varStartDateTime As DateTime
            varStartDateTime = txtStartTime.Text
    
            Try
                Dim Cnn As New OleDb.OleDbConnection("File Name=E:\Web\rhi\UDL\clockno.udl")
                Dim Command As New OleDb.OleDbCommand("INSERT INTO tbStartTime(dbClockNo, dbName, dbDate, dbDateTime) VALUES(@dbClockNo, @dbName, @dbDate, @dbDateTime)", Cnn)
    
    
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbClockNo", OleDb.OleDbType.VarChar, 10))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbName", OleDb.OleDbType.VarChar, 25))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbDate", OleDb.OleDbType.Date))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbDateTime", OleDb.OleDbType.Date))
    
                Command.Parameters("@dbClockNo").Value = Me.gvName.SelectedDataKey.Item(0)
                Command.Parameters("@dbName").Value = Me.gvName.SelectedDataKey.Item(1)
                Command.Parameters("@dbDate").Value = varStartDateTime.ToString("d")
                Command.Parameters("@dbDateTime").Value = varStartDateTime.ToString("g")
    
                Cnn.Open()
                Command.ExecuteNonQuery()
                Cnn.Close()
    
                lblError.Text = ""
            Catch ex As Exception
                'lblError.Text = "Error: Start Time Was Not Inserted"
                lblError.Text = ex.Message
            End Try
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 9, 2012 8:27 PM

All replies

  • User1929035129 posted

    which exception or error

    always elaborate more it will save time of everyone

    Monday, April 9, 2012 1:59 PM
  • User-1812511197 posted

    I thought for sure I had pasted in there.  sorry..

    Here is the exception.....

    Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.

    Monday, April 9, 2012 5:50 PM
  • User-1812511197 posted

    I figured it out.  I should have seen this.

    I need to format the variable not the text box.

    Here is the corrected code.

            Dim varStartDateTime As DateTime
            varStartDateTime = txtStartTime.Text
    
            Try
                Dim Cnn As New OleDb.OleDbConnection("File Name=E:\Web\rhi\UDL\clockno.udl")
                Dim Command As New OleDb.OleDbCommand("INSERT INTO tbStartTime(dbClockNo, dbName, dbDate, dbDateTime) VALUES(@dbClockNo, @dbName, @dbDate, @dbDateTime)", Cnn)
    
    
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbClockNo", OleDb.OleDbType.VarChar, 10))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbName", OleDb.OleDbType.VarChar, 25))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbDate", OleDb.OleDbType.Date))
                Command.Parameters.Add(New OleDb.OleDbParameter("@dbDateTime", OleDb.OleDbType.Date))
    
                Command.Parameters("@dbClockNo").Value = Me.gvName.SelectedDataKey.Item(0)
                Command.Parameters("@dbName").Value = Me.gvName.SelectedDataKey.Item(1)
                Command.Parameters("@dbDate").Value = varStartDateTime.ToString("d")
                Command.Parameters("@dbDateTime").Value = varStartDateTime.ToString("g")
    
                Cnn.Open()
                Command.ExecuteNonQuery()
                Cnn.Close()
    
                lblError.Text = ""
            Catch ex As Exception
                'lblError.Text = "Error: Start Time Was Not Inserted"
                lblError.Text = ex.Message
            End Try
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 9, 2012 8:27 PM