Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > Visual Basic 2010 General Discussion
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion 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 December 10th, 2011, 11:16 AM
Registered User
 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question [VB 2010] InvalidCastException was unhandled by user code

Hi,

I use this code to populate combobox1&2:

Code:
 
'Main form on load events
    Private Sub frm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Connect to Access Database
        dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = C:\DB.mdb;Jet OLEDB:Database Password=Pass;"
        con.ConnectionString = dbProvider & dbSource
        con.Open()

        'Call populate combobox procedure
        popCbo1()
        popCbo2()
        popCbo3()

    End Sub

    Private Sub popCbo1()
        sql1 = "SELECT DISTINCT location FROM Table ORDER BY location ASC"
        da1 = New OleDb.OleDbDataAdapter(sql1, con)
        da1.Fill(ds1, "Tab1")
        popCbo1.DataSource = ds1.Tables(0)
        popCbo1.DisplayMember = "location"
        popCbo1.ValueMember = "location"
    End Sub
    
    Private Sub popCbo2()
        sql2 = "SELECT DISTINCT director FROM Table WHERE lcoation = '" & popCbo1.SelectedValue & "' ORDER BY director"
        da2 = New OleDb.OleDbDataAdapter(sql2, con)
        da2.Fill(ds2, "Tab2n")
        popCbo2.DataSource = ds2.Tables(0)
        popCbo2.DisplayMember = "director"
        popCbo2.ValueMember = "director"
    End Sub
and to cascade result:

Code:
   
Private Sub popCbo1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles popCbo1.SelectedIndexChanged
        popCbo2()
    End Sub

    Private Sub popCbo1_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles popCbo1.SelectionChangeCommitted
        popCbo2()
    End Sub
but in the end I receive this error message:

InvalidCastException was unhandled by user code

and points to:
Code:
sql2 = "SELECT DISTINCT director FROM Table WHERE lcoation = '" & popCbo1.SelectedValue & "' ORDER BY director"
How to fix this, please?


Regards





Similar Threads
Thread Thread Starter Forum Replies Last Post
nullreferenceexception was unhandled by user code rnavin BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 June 11th, 2011 09:26 AM
ConstraintException was unhandled by user code jcooper0 BOOK: Professional ASP.NET MVC 2 8 February 16th, 2011 09:32 AM
Problem need help(!!!) ConstraintException was unhandled bt user code west BOOK: Professional ASP.NET MVC 2 3 October 26th, 2010 01:52 PM
HttpCompileException was unhandled by user code. error CS0246 fh84 BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 4 October 25th, 2010 12:49 PM
TargetInvocationException was unhandled by user code MattHoff BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 2 October 13th, 2010 11:38 AM





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