Data in listview (uwp)

1

How to display information from a MySQL database record in a listview?

I tried it like this but it did not work.

using (_connection = new MySql.Data.MySqlClient.MySqlConnection("Database=teste; Data Source=192.168.0.17;User Id=RFID;Password=1234;SslMode=None;"))
        {
            System.Text.EncodingProvider ppp;
            ppp = System.Text.CodePagesEncodingProvider.Instance;
            Encoding.RegisterProvider(ppp);

            _connection.Open();
            var cmd = new MySqlCommand("SELECT distinct id, Placa FROM test", _connection);

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {

                    listView.Items.Add("Registro: " +reader.GetString("id") + reader.GetString("\nPlaca"));


                }
            }
        }
    
asked by anonymous 14.03.2017 / 14:25

1 answer

1

The method GetString of DataReader expects int as parameter and you are passing the field name. There are two ways to fix the error:

If you want to use GetString the field names should be changed for your indexes in the query:

listView.Items.Add("Registro: " +reader.GetString(0) + reader.GetString(1));

If you want to use the name of the fields you should use the GetOrdinal method, thus:

listView.Items.Add("Registro: " +reader.GetOrdinal("id") + reader.GetOrdinal("Placa"));
    
14.03.2017 / 14:40