none
C# mysql example Command.ExecuteReader throws exception

    Question

  •  am writing a segment of C# code to get some data from mysql database. I have started with ready-made example but it gave me an error at rdr = cmd.ExecuteReader()

    using System;
    using MySql.Data.MySqlClient; 
    
    public class Program
    {
    
        static void Main() 
        {
                string serverName = "192.168.0.012";
                string port = "1234";
                string db = "cust_db";
                string userID = "root";
                string password = "pass";
                string cs = "Server=" + serverName + ";Port=" + port + ";Database=" + db + ";Uid=" + userID + ";password=" + password;
    
    
            MySqlConnection conn = null;
            MySqlDataReader rdr = null;
    
            try 
            {
                conn = new MySqlConnection(cs);
                conn.Open();
    
                string stm = "SELECT * FROM cust_tb";
                MySqlCommand cmd = new MySqlCommand(stm, conn);
                rdr = cmd.ExecuteReader();
    
                while (rdr.Read()) 
                {
                    Console.WriteLine(rdr.GetInt32(0) + ": " 
                        + rdr.GetString(1));
                }
    
            } catch (MySqlException ex) 
            {
                Console.WriteLine("Error: {0}",  ex.ToString());
    
            } finally 
            {
                if (rdr != null) 
                {
                    rdr.Close();
                }
    
                if (conn != null) 
                {
                    conn.Close();
                }
    
            }
        }
    }    
    
    
    
    
    
    
    the error that I am recieving is:
    An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
    Additional information: The given key was not present in the dictionary.
    The program '[17084] csMysqlConnection.vshost.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).

    Sunday, December 09, 2012 8:57 AM

All replies

  • As you are using MySQL, there could be some issue with that.

    So first try connecting MySql through Visual Studio Server Explorer with an ODBC connection. You will able to find the root cause of the issue.


    Please mark it as an answer/helpful if you find it as useful. Thanks, Satya Prakash Jugran

    Sunday, December 09, 2012 9:11 AM
  •  string cs = "Server=" + serverName + ";Port=" + port + ";Database=" + db + ";Uid=" + userID + ";password=" + password;
    

    Please check the "Server", "Port" or other keywords in the connection string. It might be wrong and can not be found.

    Sunday, December 09, 2012 9:47 AM
  • I do not think there is a problem with the connection since I could run the following query "SELECT VERSION()" successfully 

    Sunday, December 09, 2012 9:52 AM
  • Provide your table structure/schema of the table : cust_tb

    There is a problem in populating the fields of this table through ExecuteReader() command.


    Please mark it as an answer/helpful if you find it as useful. Thanks, Satya Prakash Jugran

    Sunday, December 09, 2012 9:57 AM
  • table name: cust_tb

    cust_id (integer) 

    cust_name (varchar)

    company (varchar)

    Sunday, December 09, 2012 11:05 AM
  • table name: cust_tb

    cust_id (integer) 

    cust_name (varchar)

    company (varchar)


    There could be some problem in MySql.Data.MySqlClient library.

    You can check the same with the documentation for MySql.Data.MySqlClient library.


    Please mark it as an answer/helpful if you find it as useful. Thanks, Satya Prakash Jugran

    Sunday, December 09, 2012 11:46 AM
  • (I am using MySQL Connector Net 6.5.4) This link may be a helpful resource for you: 
    http://dev.mysql.com/downloads/connector/net/

    You may find some examples from : http://wildclick.wordpress.com/codelib/codelib-c/

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.Data;
    using MySql.Data.Common;
    using MySql.Data.Types;
    using MySql.Data.MySqlClient;
    
    namespace MySQLDatabaseConnectivityTestConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strMySqlConnectionString =
                    @"SERVER=localhost;" +
                    @"DATABASE=<database_name>;" +
                    @"UID=<user_id>;" +
                    @"PASSWORD=<password>";
                MySqlConnection MSqlConnDummy = new MySqlConnection(strMySqlConnectionString);
                MySqlCommand mSqlCmdSelectCustomers = MSqlConnDummy.CreateCommand();
                mSqlCmdSelectCustomers.CommandText = @"select * from customers limit 10";
                MySqlDataReader mSqlReader_Customers;
                MSqlConnDummy.Open();
                mSqlReader_Customers = mSqlCmdSelectCustomers.ExecuteReader();
                DataTable dtCustomers = new DataTable();
                dtCustomers.Load(mSqlReader_Customers);
                foreach (DataRow row in dtCustomers.Rows)
                {
                    Console.WriteLine(row["customerName"].ToString());
                }
                Console.ReadKey();
            }
        }
    }

    for web based application please visit my code library for asp.net http://wildclick.wordpress.com/codelib/codelib-aspnet/


    Aarsh Talati

    Sunday, December 09, 2012 3:29 PM
  • This url demonstrates the step by step on how to connect to remote MySQL database : http://forums.devshed.com/mysql-help-4/cant-connect-to-mysql-from-remote-computer-using-c-739964.html

    Aarsh Talati

    Sunday, December 09, 2012 3:50 PM
  • i used the code and connector you recommended but same exception has been generated :(

    Monday, December 10, 2012 4:45 AM
  • i used the code and connector you recommended but same exception has been generated :(

    You were saying SELECT VERSION() was running successfully.

    Have you tried running with other tables e.g "select * from <any other table>"


    Please mark it as an answer/helpful if you find it as useful. Thanks, Satya Prakash Jugran

    Monday, December 10, 2012 5:32 AM
  • yes but it throws the same exception
    Tuesday, December 11, 2012 3:53 AM