Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 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 March 19th, 2006, 03:14 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
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?

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
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



All times are GMT -4. The time now is 03:04 AM.


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