Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 3rd, 2003, 05:28 PM
Registered User
 
Join Date: Jun 2003
Location: Manila, Laguna, Philippines.
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


thnx

HEY_HEY
Reply With Quote
  #2 (permalink)  
Old June 3rd, 2003, 08:48 PM
Registered User
 
Join Date: Jun 2003
Location: Hampton, NH, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
Reply With Quote
  #3 (permalink)  
Old June 4th, 2003, 09:52 AM
Registered User
 
Join Date: Jun 2003
Location: Manila, Laguna, Philippines.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwincusi
Default

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...
        objConnection.Open()

        '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
        Else
            '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) " & _
            "VALUES(?,?,?,?,?);"

        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...
        Try
            objCommand.ExecuteNonQuery().ToString()


        Catch err As OleDbException
            MessageBox.Show(err.Message)
        End Try

        'close the connection...
        objConnection.Close()

        'fill the dataset and bind the fields...
        FillDataSetAndView()
        BindFields()

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

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


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




    End Sub

HEY_HEY
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.