0

I use c# connect to xampp database. And it can insert normally but. When i try to show data from database in listview. So it will show "Index was outside the bounds of the array". https://www.dropbox.com/s/whl9jduwwed2swz/8.PNG?dl=1

private void show()
            {
            string connectionString = 
"datasource=127.0.0.1;port=3306;username=root;password=;database=testmg;";
        // Select all
        string query = "SELECT * FROM car";

        MySqlConnection databaseConnection = new MySqlConnection(connectionString);
        MySqlCommand commandDatabase = new MySqlCommand(query, databaseConnection);
        commandDatabase.CommandTimeout = 60;
        MySqlDataReader reader;

        try
        {
            databaseConnection.Open();
            reader = commandDatabase.ExecuteReader();
            // Success, now list 

            // If there are available rows
            if (reader.HasRows)
            {
                while (reader.Read())
                {

                    Console.WriteLine(/*reader.GetChar(0) + " - " + reader.GetChar(1) + " - " + */reader.GetChar(2) + " - " + reader.GetChar(3) + " - " + reader.GetChar(4) + " - " + reader.GetChar(5));
                    //Example to save in the listView1 :
                    string[] row = {/* reader.GetString(0), reader.GetString(1), */reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetString(5) };
                    var listViewItem = new ListViewItem(row);
                    listView1.Items.Add(listViewItem);
                }
            }
            else
            {
                Console.WriteLine("No rows found.");
            }

            databaseConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
James Cass
  • 13
  • 1
  • 5
  • What line does the exception occur on? – PaulF Aug 23 '17 at 12:13
  • Not sure but When i try to show the data base in c#. it error like the picture on my dropbox above the code. The message may show from catch exception MessageBox.Show(ex.Message); – James Cass Aug 23 '17 at 12:39
  • "Not sure" ???? - have you tried using the debugger to find out where the code fails? – PaulF Aug 23 '17 at 12:41
  • It is not show the error. but i didnot get any data from database. And it show the exception database. – James Cass Aug 23 '17 at 12:48
  • What exception did it show about the database - you need to give as much information as possible if you want any help. Possibly you have a problem with you connection string - do you need a password? Have you tried connecting to the database either with MySQL workbench or the command line & running your query there to see what results you get? – PaulF Aug 23 '17 at 12:57
  • I can insert into database. But when i try to select it will show "Index was outside the bounds of the array" – James Cass Aug 23 '17 at 13:12
  • Thanks, i just comment the Console.WriteLine. And it work now. :) – James Cass Aug 23 '17 at 13:18
  • I would have expected that to cause an error in the debugger as well. – PaulF Aug 23 '17 at 16:31

0 Answers0