Reputation:
I am using Windows Forms from Visual Studio. Coding with C#
I would like help with adding listbox items to textboxes. The code is as follows:
//*When a button is clicked, it will display details of every customer into a listbox. Each row has a customer's firstname, lastname, phone, and email address. Customer's details come from a database.
string customerFirstname = "";
string customerLastname = "";
string customerPhone = "";
string customerEmail = "";
private void buttonSearch_Click(object sender, EventArgs e)
{
SQL.selectQuery("SELECT * FROM Customer");
if (SQL.read.HasRows)
{
//this goes through each table row in the database
while (SQL.read.Read())
{
//the values are obtained from the database and assigned to the variables.
customerFirstname = SQL.read[0].ToString();
customerLastname = SQL.read[1].ToString();
customerPhone = SQL.read[2].ToString();
customerEmail = SQL.read[3].ToString();
//Each row is displayed in the listbox
listBoxCustomers.Items.Add(
customerFirstname
+ customerLastname + customerPhone + customerEmail);
}
}
}
When a row has been selected, I would like click a button and display that particular customer's details in textboxes.
This is the code I have used:
private void buttonSelectCustomerFromSearch_Click(object sender, EventArgs e) {
textboxCustomerFirstName.Text = customerFirstname;
textboxCustomerLastname.Text = customerLastname;
textboxCustomerPhone.Text = customerPhone;
textboxCustomerEmail.Text = customerEmail; }
I'm having trouble with adding the values from the selected listbox item to the textbox. When I run this, the textbox shows the details of the last customer in the database, and it ignores any selected rows. Can you please help? Thanks.
Upvotes: 0
Views: 744
Reputation: 164
I think this is a simple demo to start with
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private readonly List<Customer> customers = GetCustomers();
private static List<Customer> GetCustomers()
{
var customers = new List<Customer>();
using (var con = new SqlConnection(connectionString))
{
string sql = "SELECT Id, FirstName, LastName, Email, Phone FROM Customers";
using (var cmd = new SqlCommand(sql, con))
{
cmd.CommandType = CommandType.Text;
try
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var customer = new Customer
{
Id = Convert.ToInt32(reader["Id"]),
FirstName = reader["FirstName"].ToString(),
LastName = reader["FirstName"].ToString(),
Email = reader["Email"].ToString(),
Phone = Convert.ToInt32(reader["Phone"]),
};
customers.Add(customer);
}
}
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
}
}
}
return customers;
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = customers;
listBox1.DisplayMember = "FirstName";
listBox1.ValueMember = "Id";
}
private void button1_Click(object sender, EventArgs e)
{
int Id = (int)listBox1.SelectedValue;
Customer customerSelected = customers.Find(el => el.Id == Id);
textBox1.Text = customerSelected.ToString();
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Phone { get; set; }
public string Email { get; set; }
public int Id { get; set; }
public override string ToString()
{
return $"Id: {Id},FirstName: {FirstName},LastName: { LastName},Phone: {Phone},Email: {Email}";
}
}
Upvotes: 0
Reputation: 623
In your click handler, you are using "customerFirtName", "customerLastname", etc.. These are the variables you used to read the data from the database. This is why your textbox shows the data from the last customer from your database - it was the last one transferred using that variables, and these variables still hold its data.
you want the data of the listboxes "Selected Item" instead. you can either parse the data out of this items texts, or you could make a "person" object and store it in the listboxes "tag". So, when the button gets clicked, you could cast the listbox.SelectedItem.Tag to the person type and use that to fill the textbox.
Upvotes: 1