1

I have a MySQL database containing of images and texts that want binding it to ListBox in WPF project; here is my code that have exception error, how can I fix that?

enter image description here

public DataTable GetTable(String query, String sortBy)
{
    String connString = "server=localhost;uid=root;pwd=root;database=database_clothes";
    connection = new MySqlConnection(connString);
    adapter = new MySqlDataAdapter(query, connection);
    DataTable dataTable = new DataTable();

    adapter.Fill(dataTable);
    dataTable.DefaultView.Sort = sortBy;
    return dataTable;
}
tabby
  • 1,878
  • 1
  • 21
  • 39
faraday
  • 43
  • 9

2 Answers2

1

Just answering my question! the problem was not mysql authentication despite of error message, here was my problem in sql commands in xaml file where the table name "names" was not as mine:

        <ObjectDataProvider x:Key="NamesTable"
ObjectType="{x:Type local:DatabaseTable}"
MethodName="GetTable">
        <ObjectDataProvider.MethodParameters>
            <s:String>SELECT * FROM names</s:String>
            <s:String>Name</s:String>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
faraday
  • 43
  • 9
0

this exception said taht your connection is invalid. i think the database name is not correct. after resolve this problem , you should map your datatable to observablecollection then can binde observablecollection to your ListBox

here you can find good description

How to bind ObservableCollection with Listbox in WPF

mm8
  • 163,881
  • 10
  • 57
  • 88