Wrox Programmer Forums
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 3rd, 2003, 05:28 PM
Registered User
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwincusi
Default Help in Chapter 16

i tried to follow the exercises in chapter 16 I am already at adding records and I am encountering this error

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Invalid cast from System.String to System.TimeSpan.

can you give me an idea on this any help would be greatly appreciated


Old June 3rd, 2003, 08:48 PM
Registered User
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

I am not up to that section but the error sounds familiar. This happened to me when I had OPTION STRICT ON set and inadvertently tried to convert an object of one type into another implicitly. With OPTION STRICT OFF you can do some types of conversions without worrying about it. Since I cannot see the code you are writing, I suggest you use a try catch and set the messaging on. You can find the details in the help if you search for InvalidCastException.

What are you doing with System.Timespan? The datediff function uses this?
Old June 4th, 2003, 09:52 AM
Registered User
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwincusi

here is the code i modified it for my own purpose

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        'declare local variables and objects...
        Dim intPosition As Integer, intMaxID As Integer
        Dim strID As String
        Dim objCommand As OleDbCommand = New OleDbCommand()

        'save the current record position...
        intPosition = objCurrencyManager.Position

        'initialize a new instance of the dataset object...
        objDataSet = New DataSet()

        'open the database connection...

        'initialize a new instance of oledbadapter object...
        objDataAdapter = New OleDbDataAdapter( _
            "SELECT MAX(BillNO) as MaxID " & _
            "FROM Logs WHERE BillNo LIKE 'DM%'", objConnection)

        'fill the dataset object with data...
        objDataAdapter.Fill(objDataSet, "Logs")

        'if maxid column is null...
        If objDataSet.Tables("Logs").Rows(0).Item("MaxID") Is _
                    System.DBNull.Value Then
            'set a default value of 1000...
            intMaxID = 1000
            'otherwise set the strid variable to the value of MaxID...
            strID = CStr(objDataSet.Tables("Logs").Rows(0).Item("MaxID "))
            'get the integer part of the string...
            intMaxID = CInt(strID.Remove(0, 2))
            'increment the value...
            intMaxID += 1
        End If

        'finally set the new id...
        strID = intMaxID

        'set the oledbcommand object properties.
        objCommand.Connection = objConnection
        objCommand.CommandText = "INSERT INTO Logs " & _
            "(BillNo,StartTime,EndTime,ComputerUsage,Amoun t) " & _

        objCommand.CommandType = CommandType.Text

        'add parameters for the placeholders in the sql in the
        'commandtext property...

        'parameter fo rthe BillNo...
        objCommand.Parameters.Add(New OleDbParameter())
        objCommand.Parameters.Item(0).Direction = ParameterDirection.Input
        objCommand.Parameters.Item(0).DbType = DbType.Int16
        objCommand.Parameters.Item(0).Value = strID

        'parameter for the Login...
        objCommand.Parameters.Add(New OleDbParameter())
        objCommand.Parameters.Item(1).Direction = ParameterDirection.Input
        objCommand.Parameters.Item(1).DbType = DbType.Time

        objCommand.Parameters.Item(1).Value = txtLogin.Text

        'parameter for the Logout...
        objCommand.Parameters.Add(New OleDbParameter())
        objCommand.Parameters.Item(2).Direction = ParameterDirection.Input
        objCommand.Parameters.Item(2).DbType = DbType.Time

        objCommand.Parameters.Item(2).Value = txtLogout.Text

        'parameter for the Timeused...
        objCommand.Parameters.Add(New OleDbParameter())
        objCommand.Parameters.Item(3).Direction = ParameterDirection.Input
        objCommand.Parameters.Item(3).DbType = DbType.Time

        objCommand.Parameters.Item(3).Value = txtTimeUse.Text

        'parameter for the Amount
        objCommand.Parameters.Add(New OleDbParameter())
        objCommand.Parameters.Item(4).Direction = ParameterDirection.Input
        objCommand.Parameters.Item(4).DbType = DbType.Currency

        objCommand.Parameters.Item(4).Value = txtAmount.Text

        'execute the oledbcommand object to insert the new data...

        Catch err As OleDbException
        End Try

        'close the connection...

        'fill the dataset and bind the fields...

        'set the record position to the one that we saved
        objCurrencyManager.Position = intPosition

        'display a message that the record was added...

        'clean up...
        objDataSet = Nothing
        objDataAdapter = Nothing
        objCommand = Nothing

    End Sub


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 16 Fig 16-11 krsouthern BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 1 July 8th, 2008 12:11 PM
Chapter 16 boyce0324 VB.NET 2002/2003 Basics 1 June 5th, 2007 01:59 AM
Chapter 16 czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 3 April 11th, 2005 11:01 AM
chapter 16 ?? farsfar2 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 11 March 26th, 2005 10:01 PM
Chapter 16 ioda006 JSP Basics 3 August 21st, 2004 07:28 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.