Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show listbox item in textBox using data from local database in C#
    primarykey
    data
    text
    <p>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.</p> <p>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".</p> <pre><code>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&lt;int&gt;("ID"), FirstName = row.Field&lt;string&gt;("Firsname"), LastName = row.Field&lt;string&gt;("Lastname"), FullName = row.Field&lt;string&gt;("Firstname") + " " + row.Field&lt;string&gt;("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 &amp;&amp; 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(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload