Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 March 19th, 2006, 03:14 PM
Registered User
 
Join Date: Feb 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 'System.InvalidCastException' Problem! Please help

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:
Code:
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:
Quote:
quote: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?






Similar Threads
Thread Thread Starter Forum Replies Last Post
System.InvalidCastException; System.Reflection.Tar Jophie BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 January 16th, 2008 03:29 PM
System.InvalidCastException: Specified cast is not scslarry BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 6 January 18th, 2007 07:34 PM
PhileIdentity - System.InvalidCastException terencetham BOOK: ASP.NET Website Programming Problem-Design-Solution 6 September 25th, 2006 02:27 PM
System.InvalidCastException: QueryInterface for sweta .NET Web Services 5 June 8th, 2006 02:32 AM
System.InvalidCastException: Specified cast is not chiefg BOOK: ASP.NET Website Programming Problem-Design-Solution 2 March 2nd, 2004 01:29 PM





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