SourceForge has been redesigned. Learn more.

Invalid Cast in DataAdapter.fill

  • Robert Hammond

    Robert Hammond - 2003-09-26

    I am receiving the following error when trying to fill a dataset using the code:
    Dim conn As MySQLConnection = New MySQLConnection()

            Dim strconn As MySQLConnectionString = New MySQLConnectionString("localhost", "checkbook", "root", "par~can")
            conn.ConnectionString = strconn.AsString

            MsgBox("Connection Open")

            Dim NewCommand As MySQLCommand = conn.CreateCommand()
            NewCommand.CommandType = CommandType.Text
            NewCommand.CommandText = "SELECT * FROM transactions;"

            Dim Adp As MySQLDataAdapter = New MySQLDataAdapter()
            Adp.SelectCommand = NewCommand

            Dim ds As DataSet = New DataSet()

            MsgBox("Connection closed")

    Error at Adp.Fill(ds):

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

    Additional information: Specified cast is not valid.

    Is there something I am doing wrong?  I am somewhat new and have not been able to find any information in any previous posts regarding this.
    Thanks for any help anyone can give.

    • Anonymous

      Anonymous - 2004-01-07

      I have the same problem in a project I am working on. Anybody have a  solutiop ?

    • Ellery Familia

      Ellery Familia - 2004-01-29

      sounds like a bug too... try not opening the connection "conn" explicitly... Adp.Fill does that automatically... ether way, you guys should submit this as a bug..

      and Robert, I hope "par~can" isn't really your password. :) you should change it if it is.

    • Anonymous

      Anonymous - 2004-02-03

      I write MySQLDataAdapter to MySQLDriverCS and yes.. this error ocurred if you create MySQLCommand using conn.CreateCommand(). CreateCommand return IdbCommand type and MySqlDataAdapter.SelectCommand set and get a MySQLCommand type.

      Try Use this:
      Dim Adp As MySQLDataAdapter = New MySQLDataAdapter((MySQLCommand)NewCommand)

      see you
      Omar del Valle Rodrguez

    • Arthenius

      Arthenius - 2004-03-31

      I have this problem too...

      and the :

      Dim Adp As MySQLDataAdapter = New MySQLDataAdapter((MySQLCommand)NewCommand)

      doesn't change anything...



    • eric677

      eric677 - 2004-04-22

      I have the same problem.....

      Here is my code:

      MF = (MainForm) MainForm.ActiveForm;
      string Source = "select * from invoice order by no";
      MySQLDataAdapter InvoiceAdapter = new MySQLDataAdapter(Source, MF.MyConnection1);
      InvoiceAdapter.Fill(MF.DS, "Invoice");

      and have the same error.....
      can anyone help???

    • Karl-Thomas Schmidt

      Tried it for nearly two weeks.
      Stuck to the same failure.

      Anybody got arround this failure?


    • rt

      rt - 2004-07-03

      I have the same problem ("Specified cast is not valid") when trying to fill in a dataset from the data adapter. 

      All i want to do it display the results of a query in a datagrid control, so here is my workaround, i hope it helps:

      MySQLConnection con;       
      con = new MySQLConnection(new MySQLConnectionString("localhost", "my_db", "root", "").AsString);

      string sql = "SELECT * FROM table";
      MySQLCommand cmd = new MySQLCommand(sql,con); 
      MySQLDataReader reader = cmd.ExecuteReaderEx();

      DataTable myTable = new DataTable("blah");
      for (int i = 0; i < reader.FieldCount; i++) {
      while (reader.Read()) {
           String[] rt = new String[reader.FieldCount];
           for (int i = 0; i < reader.FieldCount; i++) {
                rt[i] = reader.GetString(i);

      DataView myDataView = new DataView( myTable );
      this.dataGrid1.DataSource = myDataView;


Log in to post a comment.