Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Specified cast is not valid

Specified cast is not valid

Scheduled Pinned Locked Moved Visual Basic
helpquestionannouncementcsharpdatabase
30 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Colin Angus Mackay

    I get the impression that his code is compiling. The message he mentions suggests that he has got the code running. A missing referrence would mean the code wouldn't compile. Of course, he could be running the binaries he compiled at uni'.


    * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

    S Offline
    S Offline
    Stephen McGuire
    wrote on last edited by
    #9

    Yes. I agree. Just a thought... Steve

    1 Reply Last reply
    0
    • S Stephen McGuire

      You should be notified in the Task List if your project detects a missing reference. What line of code is causing the error? It is not highlited as you stated in your original post. Steve

      N Offline
      N Offline
      newbjohny
      wrote on last edited by
      #10

      No I am not notified of a missing reference, sorry, I did highlight the line but then when I put the code in code tags it must have got rid of the bold, the line that causes the error is this one :- dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName) John

      S 1 Reply Last reply
      0
      • C Colin Angus Mackay

        newbjohny wrote:

        I have been taught this way at University so as not to crash out the program.

        Your try/catch blocks here are being used for debugging purposes not to prevent the application from crashing. Using try/catch for debugging is a waste of time when there are better tools available. Did your university not teach you how to set breakpoints and step through code and how to watch variables? You need to find out the full stack trace of where the exception was thrown. By catching the exception and displaying only the message you've thrown all that information away - get rid of the try/catch and see where the application actaully crashes. When it does the Visual Studio debugger will show you exactly where the crash is (even if it is in code you did not write) If you are missing a referrence your application is unlikely to compile on your home machine.


        * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        N Offline
        N Offline
        newbjohny
        wrote on last edited by
        #11

        Hi, sadly they did not teach us to do anything other that the try and catch, to be honest they never really taught us, all the lecture notes were ripped straight from the recommended text book. Anyway thats a aside issue. I've taken out the try-catch and it still crashes out with the same error, but two lines of code are highlighted, the one mentioned earlier in the form :- dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName) and one in selectAll function in the connection class:- sqlite_dataAdapter.Fill(dset, tableName) A third line is highlighted in dissassembly but I have no idea what this is :- 00000014 mov ecx,eax

        C 1 Reply Last reply
        0
        • N newbjohny

          No I am not notified of a missing reference, sorry, I did highlight the line but then when I put the code in code tags it must have got rid of the bold, the line that causes the error is this one :- dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName) John

          S Offline
          S Offline
          Stephen McGuire
          wrote on last edited by
          #12

          What is 'dbConn'? I'm confused how selectAll is a method of dbConn... Steve

          N 1 Reply Last reply
          0
          • N newbjohny

            Hi, sadly they did not teach us to do anything other that the try and catch, to be honest they never really taught us, all the lecture notes were ripped straight from the recommended text book. Anyway thats a aside issue. I've taken out the try-catch and it still crashes out with the same error, but two lines of code are highlighted, the one mentioned earlier in the form :- dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName) and one in selectAll function in the connection class:- sqlite_dataAdapter.Fill(dset, tableName) A third line is highlighted in dissassembly but I have no idea what this is :- 00000014 mov ecx,eax

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #13

            Ignore the disassembly - what is in the call stack? It should be in the lower half of the Visual Studio window. (Use the following to show it if it is not displayed: Debug-->Windows-->Call Stack)

            newbjohny wrote:

            dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName)

            What is dbConn? I would assume it is a connection, but selectAll is (1) Not a standard method on a connection class and (2) does not conform to the .NET naming guidelines so it may have been written by someone with a Java background.


            * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

            N 1 Reply Last reply
            0
            • N newbjohny

              Hi, I am using VS.Net 2002 edition with the 1.1 framework both at University and at home (my home copy is from University using the University licence so I presume is exactly the same) I have created a front-end application for SQLite (an open source DBMS) which creates databases for users with no SQL knowledge. I link to the DBMS using a .dll file. When I fill a dataset with the contents of a table at University it works fine then goes onto fill a datagrid with this information. Yet when I run the same application at home I get a "Specified cast is not valid" error. Not one thing changes to the application, the data in the table, the .dll file, everything is the same in the two instances yet one works and the other doesn't. Does anyone know what is wrong, if I need to change a setting or install an update on my home PC or something? Any help is greatly appreciated. I have included my code and highlighted where the error shows up although I do not believe my code to be the problem as like I said it works on the University PC's. Many thanks John Code for populating dataset and datagrid Private Sub cmbInsertData_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbInsertData.SelectedIndexChanged Dim dset As DataSet '= New DataSet() If Len(cmbInsertData.SelectedItem) < 1 Then MessageBox.Show("Please choose a table to view") Else Try tblName = (cmbInsertData.Text) setTableName(tblName) dbConn.openExistingDatabse("Data Source=" & getDBName() & ";Version=3;New=False;Compress=True;") dbConn.createSQLCommand() Try dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName) Catch es As Exception MessageBox.Show("error here -" & es.Message) End Try StoreDataSet.setDataSet(dset) DataGrid1.DataSource = dset.Tables(0).DefaultView Catch es As Exception MessageBox.Show(es.Message) End Try End If End Sub Public Function selectAll(ByVal strSQL As String, ByVal tableName As String) Dim dset As DataSet = New DataSet() sqlite_cmd.CommandText = (strSQL) sqlite_datareader = sqlite_cmd.ExecuteReader() Dim sqlite_dataAdapter As SQLiteDataAdapter = New SQLiteDataAdapter(strSQL, sqlite_conn) sqlite_co

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #14

              newbjohny wrote:

              Private Sub cmbInsertData_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbInsertData.SelectedIndexChanged

              newbjohny wrote:

              Public Function selectAll(ByVal strSQL As String, ByVal tableName As String)

              Are these two methods on the same class? -- modified at 12:04 Saturday 5th August, 2006 What are the fields (aka member variables) on the class(es)?


              * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

              N 1 Reply Last reply
              0
              • S Stephen McGuire

                What is 'dbConn'? I'm confused how selectAll is a method of dbConn... Steve

                N Offline
                N Offline
                newbjohny
                wrote on last edited by
                #15

                dbConnection is a class I created with all functions in that deal with the database. In the forms I want to connect to the database I put this line of code:- Dim dbConn As dbConnection = New dbConnection() selectAll is a function I created to select all from the given table name, put the contents into a dataSet and display in a DataGrid.

                S 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  Ignore the disassembly - what is in the call stack? It should be in the lower half of the Visual Studio window. (Use the following to show it if it is not displayed: Debug-->Windows-->Call Stack)

                  newbjohny wrote:

                  dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName)

                  What is dbConn? I would assume it is a connection, but selectAll is (1) Not a standard method on a connection class and (2) does not conform to the .NET naming guidelines so it may have been written by someone with a Java background.


                  * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                  N Offline
                  N Offline
                  newbjohny
                  wrote on last edited by
                  #16

                  The previous two messages I wrote were from the Call stack. dbConnection is a class I created with all functions in that deal with the database. In the forms I want to connect to the database I put this line of code:- Dim dbConn As dbConnection = New dbConnection() selectAll is a function I created to select all from the given table name, put the contents into a dataSet and display in a DataGrid.

                  1 Reply Last reply
                  0
                  • N newbjohny

                    dbConnection is a class I created with all functions in that deal with the database. In the forms I want to connect to the database I put this line of code:- Dim dbConn As dbConnection = New dbConnection() selectAll is a function I created to select all from the given table name, put the contents into a dataSet and display in a DataGrid.

                    S Offline
                    S Offline
                    Stephen McGuire
                    wrote on last edited by
                    #17

                    What is the definition of this 'selectAll' method? Steve

                    N 1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      newbjohny wrote:

                      Private Sub cmbInsertData_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbInsertData.SelectedIndexChanged

                      newbjohny wrote:

                      Public Function selectAll(ByVal strSQL As String, ByVal tableName As String)

                      Are these two methods on the same class? -- modified at 12:04 Saturday 5th August, 2006 What are the fields (aka member variables) on the class(es)?


                      * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                      N Offline
                      N Offline
                      newbjohny
                      wrote on last edited by
                      #18

                      No, the function is in a dbConnection class and the cmbinsertdata is on a form. I'm sorry but I do not know what a member variable is. Is there someway I could zip my code and email it to you, it may be easier if you could see the code. John

                      C 1 Reply Last reply
                      0
                      • S Stephen McGuire

                        What is the definition of this 'selectAll' method? Steve

                        N Offline
                        N Offline
                        newbjohny
                        wrote on last edited by
                        #19

                        This is the selectAll code, I just called it selectAll(select *) as that is what I am doing from the database, selecting all from a table and displaying it to the user. Public Function selectAll(ByVal strSQL As String, ByVal tableName As String) Dim dset As DataSet = New DataSet() sqlite_cmd.CommandText = (strSQL) sqlite_datareader = sqlite_cmd.ExecuteReader() Dim sqlite_dataAdapter As SQLiteDataAdapter = New SQLiteDataAdapter(strSQL, sqlite_conn) sqlite_commandBuilder = New SQLiteCommandBuilder(sqlite_dataAdapter) sqlite_dataAdapter.Fill(dset, tableName) Return dset End Function

                        S 1 Reply Last reply
                        0
                        • N newbjohny

                          This is the selectAll code, I just called it selectAll(select *) as that is what I am doing from the database, selecting all from a table and displaying it to the user. Public Function selectAll(ByVal strSQL As String, ByVal tableName As String) Dim dset As DataSet = New DataSet() sqlite_cmd.CommandText = (strSQL) sqlite_datareader = sqlite_cmd.ExecuteReader() Dim sqlite_dataAdapter As SQLiteDataAdapter = New SQLiteDataAdapter(strSQL, sqlite_conn) sqlite_commandBuilder = New SQLiteCommandBuilder(sqlite_dataAdapter) sqlite_dataAdapter.Fill(dset, tableName) Return dset End Function

                          S Offline
                          S Offline
                          Stephen McGuire
                          wrote on last edited by
                          #20

                          Are those sqlite_*** items custom components or your own classes? There is no expected line: Dim sqlite_commandBuilder As SQLiteCommandBuilder What are the 'CommandText' and 'ExecuteReader' lines doing? Nothing as far as I can see... Steve

                          N 1 Reply Last reply
                          0
                          • S Stephen McGuire

                            Are those sqlite_*** items custom components or your own classes? There is no expected line: Dim sqlite_commandBuilder As SQLiteCommandBuilder What are the 'CommandText' and 'ExecuteReader' lines doing? Nothing as far as I can see... Steve

                            N Offline
                            N Offline
                            newbjohny
                            wrote on last edited by
                            #21

                            Hi Steve, the sqlite_*** are my names I've given to them. Like in the code above, I've simply added the _ so when I come to use it elsewhere I know what it is, so SQLiteCommandBuilder becomes sqilte_commandBuilder. It's just easier for me. They come from a 3rd party wrapper which is finisar.sqlite which can be found at http://adodotnetsqlite.sourceforge.net/ the expected line of "Dim sqlite_commandBuilder As SQLiteCommandBuilder" is present and has been declared at the top of the dbConnection class with all the other things that need declaring. Now that you mention it I don't know what those two lines are doing, they are there from when I started this project a long time ago and as it was working I never took anything out. I've took them out and they makeno difference to the error messge so it looks as if they were not needed at all. -- modified at 12:47 Saturday 5th August, 2006

                            S 1 Reply Last reply
                            0
                            • N newbjohny

                              Hi Steve, the sqlite_*** are my names I've given to them. Like in the code above, I've simply added the _ so when I come to use it elsewhere I know what it is, so SQLiteCommandBuilder becomes sqilte_commandBuilder. It's just easier for me. They come from a 3rd party wrapper which is finisar.sqlite which can be found at http://adodotnetsqlite.sourceforge.net/ the expected line of "Dim sqlite_commandBuilder As SQLiteCommandBuilder" is present and has been declared at the top of the dbConnection class with all the other things that need declaring. Now that you mention it I don't know what those two lines are doing, they are there from when I started this project a long time ago and as it was working I never took anything out. I've took them out and they makeno difference to the error messge so it looks as if they were not needed at all. -- modified at 12:47 Saturday 5th August, 2006

                              S Offline
                              S Offline
                              Stephen McGuire
                              wrote on last edited by
                              #22

                              Well, I'm at a loss my friend! The only other thing I can see is the line: Dim dset As DataSet is repeated in 'cmbInsertData_SelectedIndexChanged' and in your 'selectAll' function. Don't know if that would cause an error... I suspect it has something to do with one of your third party components. Maybe one of them is corrupted or something. Good luck! Steve

                              N 1 Reply Last reply
                              0
                              • S Stephen McGuire

                                Well, I'm at a loss my friend! The only other thing I can see is the line: Dim dset As DataSet is repeated in 'cmbInsertData_SelectedIndexChanged' and in your 'selectAll' function. Don't know if that would cause an error... I suspect it has something to do with one of your third party components. Maybe one of them is corrupted or something. Good luck! Steve

                                N Offline
                                N Offline
                                newbjohny
                                wrote on last edited by
                                #23

                                It's very weird as you would suspect it to either work on both my home and Uni PC's or neither, not to work on one and not the other. Anyway thanks for your help and suggestions, they were very much appreciated. Thanks again John

                                S 1 Reply Last reply
                                0
                                • N newbjohny

                                  It's very weird as you would suspect it to either work on both my home and Uni PC's or neither, not to work on one and not the other. Anyway thanks for your help and suggestions, they were very much appreciated. Thanks again John

                                  S Offline
                                  S Offline
                                  Stephen McGuire
                                  wrote on last edited by
                                  #24

                                  Yes. This really suggests it is something other than your code. Steve

                                  N 1 Reply Last reply
                                  0
                                  • S Stephen McGuire

                                    Yes. This really suggests it is something other than your code. Steve

                                    N Offline
                                    N Offline
                                    newbjohny
                                    wrote on last edited by
                                    #25

                                    I think I should be just glad it works at University and leave it at that. Thanks again for your time and help, much appreciated. John

                                    1 Reply Last reply
                                    0
                                    • N newbjohny

                                      No, the function is in a dbConnection class and the cmbinsertdata is on a form. I'm sorry but I do not know what a member variable is. Is there someway I could zip my code and email it to you, it may be easier if you could see the code. John

                                      C Offline
                                      C Offline
                                      Colin Angus Mackay
                                      wrote on last edited by
                                      #26

                                      newbjohny wrote:

                                      I'm sorry but I do not know what a member variable is.

                                      :omg: Do they teach you anything at this university? member variable, also known as a class field, for example:

                                      public class SomeClass
                                      {
                                      // the following is a field
                                      private int someMemberVariable;
                                      }

                                      It is a variable that exists within the scope of an instance of a class. All methods and properties in the class can access the variable. You might want to invest in a couple of books that the university did not recommend.


                                      * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                                      N 1 Reply Last reply
                                      0
                                      • C Colin Angus Mackay

                                        newbjohny wrote:

                                        I'm sorry but I do not know what a member variable is.

                                        :omg: Do they teach you anything at this university? member variable, also known as a class field, for example:

                                        public class SomeClass
                                        {
                                        // the following is a field
                                        private int someMemberVariable;
                                        }

                                        It is a variable that exists within the scope of an instance of a class. All methods and properties in the class can access the variable. You might want to invest in a couple of books that the university did not recommend.


                                        * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                                        N Offline
                                        N Offline
                                        newbjohny
                                        wrote on last edited by
                                        #27

                                        Ive just never heard them be called this before, in the dbConnection class there is :- Public tableNames() As String Public indexNames() As String Public triggerNames() As String Public viewNames() As String Public columnNames As New ArrayList() Public columnTypes As New ArrayList() Dim tables, i As Integer Dim sqlite_conn As SQLiteConnection Dim sqlite_cmd As SQLiteCommand Dim sqlite_datareader As SQLiteDataReader Dim sqlite_dataAdapter As SQLiteDataAdapter Dim dset As DataSet = New DataSet() Dim sqlite_commandBuilder As SQLiteCommandBuilder Private data_Table As New DataTable() Private rowPos As Integer = 0 in the viewdata form there is :- Dim dbConn As dbConnection = New dbConnection() Dim tableName As String Dim tblName As String

                                        1 Reply Last reply
                                        0
                                        • N newbjohny

                                          Hi, I am using VS.Net 2002 edition with the 1.1 framework both at University and at home (my home copy is from University using the University licence so I presume is exactly the same) I have created a front-end application for SQLite (an open source DBMS) which creates databases for users with no SQL knowledge. I link to the DBMS using a .dll file. When I fill a dataset with the contents of a table at University it works fine then goes onto fill a datagrid with this information. Yet when I run the same application at home I get a "Specified cast is not valid" error. Not one thing changes to the application, the data in the table, the .dll file, everything is the same in the two instances yet one works and the other doesn't. Does anyone know what is wrong, if I need to change a setting or install an update on my home PC or something? Any help is greatly appreciated. I have included my code and highlighted where the error shows up although I do not believe my code to be the problem as like I said it works on the University PC's. Many thanks John Code for populating dataset and datagrid Private Sub cmbInsertData_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbInsertData.SelectedIndexChanged Dim dset As DataSet '= New DataSet() If Len(cmbInsertData.SelectedItem) < 1 Then MessageBox.Show("Please choose a table to view") Else Try tblName = (cmbInsertData.Text) setTableName(tblName) dbConn.openExistingDatabse("Data Source=" & getDBName() & ";Version=3;New=False;Compress=True;") dbConn.createSQLCommand() Try dset = dbConn.selectAll("SELECT * FROM " & tblName, tblName) Catch es As Exception MessageBox.Show("error here -" & es.Message) End Try StoreDataSet.setDataSet(dset) DataGrid1.DataSource = dset.Tables(0).DefaultView Catch es As Exception MessageBox.Show(es.Message) End Try End If End Sub Public Function selectAll(ByVal strSQL As String, ByVal tableName As String) Dim dset As DataSet = New DataSet() sqlite_cmd.CommandText = (strSQL) sqlite_datareader = sqlite_cmd.ExecuteReader() Dim sqlite_dataAdapter As SQLiteDataAdapter = New SQLiteDataAdapter(strSQL, sqlite_conn) sqlite_co

                                          M Offline
                                          M Offline
                                          mtone
                                          wrote on last edited by
                                          #28

                                          What is this? tblName = (cmbInsertData.Text) setTableName(tblName) I do not see a declaration of the Variable tblName ( Dim tblName as String ). I believe VB will create this as an Object type without the Dim statment, and then you are trying to us it as a string? what is setTableName? are you expecting the variable tblName to change when it comes out of setTableName? Step thru the code line for line in debug and you can see exactly what all your variables are and what might be missing. Joe -- modified at 0:37 Sunday 6th August, 2006

                                          N 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups