Data in listview (uwp)

Asked

Viewed 35 times

1

how to display the information of a Mysql database record in a listview?

I tried it but it didn’t 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"));


                }
            }
        }

1 answer

1


The method GetStringof DataReader expecting a int as parameter and you are passing the name of the field. There are two ways to correct the error:

If you want to use the GetString fields names should be changed to their contents in the query:

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

If you want to use the name of the fields you must use the method GetOrdinal being like this:

listView.Items.Add("Registro: " +reader.GetOrdinal("id") + reader.GetOrdinal("Placa"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.