Search code examples
c#wpfwinformssqlitecombobox

SQLite with Dapper C# - Windows Form: Using ComboBox Lisl Properties to retrieve database data with a foreign key


I work with C# in Visual Studio 2019. My database is in SQLite using Dapper.

Here is what I am struggling with.

I have 2 tables in my database that are connected. The parent, tbClient. And the child table, tbProject.

tbProject has a field to ClientId.

I use a ComboBox to WireUpp the Data from the database to my form. I have a form to Client, and a form for the Project, in this form I chose a CLient in a ComboBox, and save its ID in my tbProjet.

The idea is simple, but I am struggling because I am using an example that was made in Windows (WPF), and my application is in Windows Forms. I noticed that the Properties of the ComboBox are not the same, then I am having some trouble accessing the correct Project field when I want to open the Project of a specific Client.

Let´s show how it's done in the WPF app and in my WinForm app. I think is going to be more clear to get some help.

The codes of the Project Form are below: the first is the WPF app, and the second one is the WinForm where I was not able to make work yet.

WPF Application:

 // WPF Application:
 namespace MyApp.Controls
 {  
    public ProjectControls()
    {
        InitializeComponent();
        InitializeClientList();
        WireUpDropDowns();
    }

    private void WireUpDropDowns()
    {
        clientDropDown.ItemsSource = clients;
        clientDropDown.DisplayMemberPath = "Name";
        clientDropDown.SelectedValuePath = "Id";

        projectDropDown.ItemsSource = projects;
        projectDropDown.DisplayMemberPath = "DisplayValue";
        projectDropDown.SelectedValuePath = "Id";
    }

    private void InitializeClientList()
    {
        string sql = "select * from Client order by Name";
        var clientList = SqliteDataAccess.LoadData<ClientModel>(sql, new Dictionary<string, object>());
        clientList.ForEach(x => clients.Add(x));
    }

     private void clientDropDown_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LoadProjectDropDown();
    }
    private void LoadProjectDropDown()
    {
        string sql = "select * from tbProject where ClientId = @ClientId";

        Dictionary<string, object> parameters = new Dictionary<string, object>
        {
            { "@ClientId", clientDropDown.SelectedValue }
        };

        var records = SqliteDataAccess.LoadData<ProjectsModel>(sql, parameters);

        projects.Clear();
        records.ForEach(x => projects.Add(x));
    }
}

Windows Form Application:

     // Windows Forms Application:
 namespace MyApp.Controls
 {  
    public ProjectControls()
    {
        InitializeComponent();
        InitializeClientList();
        WireUpDropDowns();
    }

    private void WireUpDropDowns()
    {
        clientDropDown.DataSource = null;
        clientDropDown.DataSource = clients;
        clientDropDown.DisplayMember= "Name";
        clientDropDown.ValueMember = "Id";

        projectDropDown.DataSource = null;
        projectDropDown.DataSource= projects;
        projectDropDown.DisplayMember = "DisplayValue";
        projectDropDown.ValueMember= "Id";
    }

    private void InitializeClientList()
    {
        string sql = "select * from Client order by Name";
        var clientList = SqliteDataAccess.LoadData<ClientModel>(sql, new Dictionary<string, object>());
        clientList.ForEach(x => clients.Add(x));
    }

     private void clientDropDown_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LoadProjectDropDown();
    }
    private void LoadProjectDropDown()
    {
        string sql = "select * from tbProject where ClientId = @ClientId";

        Dictionary<string, object> parameters = new Dictionary<string, object>
        {
            { "@ClientId", clientDropDown.SelectedValue } 
         // --> I think the problem is here, I am passing an object to the database where ClientId is an integer type. I tried to use SelectedIndex instead, but with this property, I do not get the correct Project from the table
        };

        var records = SqliteDataAccess.LoadData<ProjectsModel>(sql, parameters);

        projects.Clear();
        records.ForEach(x => projects.Add(x));
    }
}

In the Windows Form Application I get this Error Message from my AccesDataBase Routine:

System.NotSupportedException: 'The member ClientId of type AppLibrary.Models.ClientModel cannot be used as a parameter value'

So I think the basic question here is Am I using the ComboBOx Properties correct? What I am missing?

Thank you in advance for any help received. Verônica.


Solution

  • I found out about my mistake.

    The property of the Combobox is correct.

    I was passing the wrong parameter to the ClientDropDown.SelectedValue in other parts of the code that I haven´t shared here.

    I was trying to select a specific client in the ClientDropDown through code but I was passing SelectedIndex to the SelectedValue.

    I used the Breakpoints and was able to find the error, and now is working with this code that I share here in the question, it is correct.