please help me.
Populating listbox control and textbox control from database
i have a table customer in sql server database which have three columns "id", "address", "vatno'. and in UI i have a comboBox control,a ListBox control and a TextBox control. I want when a user select "id" column from comboBox control the corresponding "address" column will populate the listBox control and "VatNo' no field will Populate the Textbox control.
Satyapriya NayakPosted Jul 23, 2013, 11:59 PM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Autopopulate_a_textbox
{
public partial class Form2 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
comboBox1.Text="Choose ID";
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from customer";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["id"].ToString());
}
con.Close();
reader.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from customer where id='" + comboBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
listBox1.Items.Add(reader["address"].ToString());
textBox1.Text = reader["vatno"].ToString();
}
con.Close();
reader.Close();
}
}
}