简体   繁体   中英

How to show listbox item in textBox using data from local database in C#

Hi im new to programming and currently working on a project that lets a user enter its registration data into a local database using textBoxes. The code works that its adding the items into the database and after i press a "Show_users" button it displays them in the listBox_users listbox.

My problem is that when i choose a name from the listBox_users it should dislay the data about the selected user in the upper textBox'es i used to enter the data in the first place using the event i created for the listBox_users, but im getting an error that "can't read data from database that is already closed".

namespace Userform
{
public partial class Form1: Form
{
    SqlCeDataReader rdr;

    public Form1()
    {
        InitializeComponent();
    }

    // Some code between...

    private void button_ShowUsers_Click(object sender, EventArgs e) //code that shows users in listBox
    {

        var dt = new DataTable();
        string connectionString2 = @"Data Source=MyDatabase;Password=xxxxxx;";

        using (var cn = new SqlCeConnection(connectionString2))
        using (var cmd = new SqlCeCommand("Select * From Users", cn))
        {
            cn.Open();

            using (var reader = cmd.ExecuteReader())
            {
                dt.Load(reader);
                var results = (from row in dt.AsEnumerable()
                               select new
                               {
                                   //UserID = row.Field<int>("ID"),
                                   FirstName = row.Field<string>("Firsname"),
                                   LastName = row.Field<string>("Lastname"),
                                   FullName = row.Field<string>("Firstname") + " " + row.Field<string>("Lastname")
                               }).ToList();

                listBox_users.DataSource = results;
                listBox_users.DisplayMember = "FullName";

                rdr = cmd.ExecuteReader();
                }
            }
        }

    //I made an event for the listBox_users:

    private void listBox_users_SelectedIndexChanged(object sender, EventArgs e) 
    //event code that should show listbox selected data in the textBoxes
    {            
        if (listBox_inimesed.SelectedItem != null && rdr != null)
        {
            try
            {
                if (rdr.Read())
                {

                    textBox1_firstname.Text = rdr.GetString(1);
                    textBox2_lastname.Text = rdr.GetString(2);
                    textBox3_email.Text = rdr.GetString(3);
                    textBox4_address.Text = rdr.GetString(4);
                    dateTimePicker1.Value = rdr.GetDateTime(5);
                    richTextBox_info.Text = rdr.GetString(6);
                }
                else MessageBox.Show("Object not found");
            }
            finally
            {
                rdr.Close();
            }
        }
    }
}

The connection that the reader you are using is dependent on has been closed in the button_ShowUsers_Click event.

It is not a good practice to try to share connections and DataReaders across events. That will result in open connections that are not disposed of correctly. A better practice would be to have the creation of the Connection, Command, and DataReader take place in each event method. By using the "using" statement they will be closed and disposed of correctly in each method. You can also remove the class level variable "rdr".

namespace Userform
{
    public partial class Form1 : Form
    {
        const string connectionString2 = @"Data Source=MyDatabase;Password=xxxxxx;";

        public Form1()
        {
            InitializeComponent();
        }

        // Some code between...

        private void button_ShowUsers_Click(object sender, EventArgs e) //code that shows users in listBox
        {
            var dt = new DataTable();

            using (var cn = new SqlCeConnection(connectionString2))
            using (var cmd = new SqlCeCommand("Select * From Users", cn))
            {
                cn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    dt.Load(reader);
                    var results = (from row in dt.AsEnumerable()
                                   select new
                                   {
                                       //UserID = row.Field<int>("ID"),
                                       FirstName = row.Field<string>("Firsname"),
                                       LastName = row.Field<string>("Lastname"),
                                       FullName = row.Field<string>("Firstname") + " " + row.Field<string>("Lastname")
                                   }).ToList();

                    listBox_users.DataSource = results;
                    listBox_users.DisplayMember = "FullName";
                }
            }
        }

    //I made an event for the listBox_users:

        private void listBox_users_SelectedIndexChanged(object sender, EventArgs e)
        //event code that should show listbox selected data in the textBoxes
        {
            if (listBox_inimesed.SelectedItem != null)
            {
                using (var cn = new SqlCeConnection(connectionString2))
                using (var cmd = new SqlCeCommand("Select * From Users", cn))
                {
                    cn.Open();

                    using (var reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            textBox1_firstname.Text = reader.GetString(1);
                            textBox2_lastname.Text = reader.GetString(2);
                            textBox3_email.Text = reader.GetString(3);
                            textBox4_address.Text = reader.GetString(4);
                            dateTimePicker1.Value = reader.GetDateTime(5);
                            richTextBox_info.Text = reader.GetString(6);
                        }
                        else MessageBox.Show("Object not found");
                    }
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM