# An unhandled exception of type 'System.InvalidCastException' occurred in Exe



## CyberGeek (Mar 19, 2006)

Ok. Well I am just playing with some code here trying to get some data from a .MDB to bind with a DataGrid, ListBox, and a ComboBox. I have one button for to bind data to the DataGrid, another for the ListBox and a third one for the ComboBox.

Then, the code I am using is:

```
Imports System.Data.OleDb
Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Creating connection and command sting
        Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\Documents and Settings\HP_Administrator\My Documents\Visual Studio Projects\Teacher Assistant ver. 0.0.1\TeacherAssistant_dbTest.mdb"
        Dim sqlStr As String = "SELECT * FROM StudentName"

        ' Create connection object
        Dim conn As OleDBConnection = New OleDBConnection(conStr)

        ' Create data adapter object
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)

        ' Create a DataSet1 object and fill with data using data adapter's Fill method
        Dim ds As DataSet1 = New DataSet
        da.Fill(ds, "StudentName")
        ' Attach DataSet1's DefaultView to the datagrid control
        DataGrid1.DataSource = ds.DefaultViewManager
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ' Creating connection and command sting
        Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\Documents and Settings\HP_Administrator\My Documents\Visual Studio Projects\Teacher Assistant ver. 0.0.1\TeacherAssistant_dbTest.mdb"
        Dim sqlStr As String = "SELECT * FROM StudentName"
        ' Create connection object
        Dim conn As OleDBConnection = New OleDBConnection(conStr)
        ' Create data adapter object
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
        ' Create a DataSet1 object and fill with data using data adapter's Fill method
        Dim ds As DataSet1 = New DataSet
        da.Fill(ds, "StudentName")

        ' Attach DataSet1's DefaultView to the datagrid control
        Dim dv As DataView = ds.Tables("StudentName").DefaultView
        ListBox1.DataSource = dv
        ListBox1.DisplayMember = "FirstName"

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        ' Creating connection and command sting
        Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\Documents and Settings\HP_Administrator\My Documents\Visual Studio Projects\Teacher Assistant ver. 0.0.1\TeacherAssistant_dbTest.mdb"
        Dim sqlStr As String = "SELECT * FROM StudentName"
        ' Create connection object
        Dim conn As OleDBConnection = New OleDBConnection(conStr)
        ' Create data adapter object
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)

        ' Create a DataSet1 object and fill with data using data adapter's Fill method
        Dim ds As DataSet1 = New DataSet
        da.Fill(ds, "StudentName")

        ' Attach DataSet1's DefaultView to the datagrid control
        Dim dv As DataView = ds.Tables("StudentName").DefaultView
        ComboBox1.DataSource = dv
        ComboBox1.DisplayMember = "FirstName"
    End Sub
End Class
```
But, I keep getting the following error message:


> An unhandled exception of type 'System.InvalidCastException' occurred in ApplicationName.exe
> 
> Additional information: Specified cast is not valid.


What do I need to do to fix that error?


----------



## CyberGeek (Mar 19, 2006)

Well no one knows an answer? Out of how many thousand registered members here, no one knows an answer?


----------

