C# Displaying a sql database object's name in a label.Text property on “SelectedIndexChanged” Event

巧了我就是萌 提交于 2019-12-12 02:56:32

问题


I am trying to make a small contact book that takes contacts details from a mssql database.It has 3 tables: contacts, last_talk(last time i talked with a contact+short description of the discussion), and another table(that has both primary keys from the first 2 tables )

On the form(tab of tabcontrol) where i display the contacts, i have added 2 listboxes, one loads and displays the contacts names, and the second listbox loads the "Last talk" list for every contact i select depending how many "talks" i had with a contact.

What i am trying to do now is: when i select a contact, i also want to display near the listboxes, some labels for the contact name, company, etc, that change their text to the database entry for the selected contact's name/company...

Here is a part of the code:

    private void lstContactList_SelectedIndexChanged(object sender, EventArgs e)
    {
        PopulateTalkList();
        PopulateContactLabels();
    }

    private void ContactBookForm_Load(object sender, EventArgs e)
    {
        PopulateContactList();

    }

    private void PopulateContactList()
    {
        string query = "SELECT * FROM Contact";
        using (connection = new SqlConnection(connectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
        {
            connection.Open();
            DataTable contactTable = new DataTable();
            adapter.Fill(contactTable);

            lstContactList.DisplayMember = "Name";
            lstContactList.ValueMember = "Id";
            lstContactList.DataSource = contactTable;


        }
    }

here is the method that i try to use to change the labels:

        private void PopulateContactLabels()
    {
        string query = "SELECT * FROM Contact";

        using (connection = new SqlConnection(connectionString))
        using (SqlCommand command = new SqlCommand(query, connection))
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            connection.Open();
            SqlDataReader rdr = command.ExecuteReader();

            while (rdr.Read())
            {
                lblContactName.Text = rdr["Name"].ToString();
                lblCompany.Text = rdr["Company"].ToString();
                lblOccupation.Text = rdr["Occupation"].ToString();
                lblPhoneNumber.Text = rdr["PhoneNumber"].ToString();
                lblEmail.Text = rdr["Email"].ToString();
            }
            rdr.Close();
            connection.Close();

        }
    }

And it does change the labels, but it selects the last contact added to the database, and it doesn't change when i select another contact.

What am i doing wrong?


回答1:


That's because in your PopulateContactLabels method, you are selecting the whole Contact table and then reading through the whole list, so it's always the last one which is shown.

You need a query more like SELECT * FROM Contact WHERE ContactId = @contactID, and then add the contactId (or whatever value you are using to find the contact) as a parameter on the SqlCommand object.



来源:https://stackoverflow.com/questions/35186589/c-sharp-displaying-a-sql-database-objects-name-in-a-label-text-property-on-sel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!