Sorry, I don't want to give you broken code, and I don't have VB.NET Express on this machine yet, so the code is in C#. The easiest way is to play the binding game.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace InannaRocks {
public class MyProgram {
public static void Main()
{
Application.Run(new MyForm());
}
}
class MyForm: Form {
private FlowLayoutPanel _container = new FlowLayoutPanel();
private TextBox _address = new TextBox();
private ComboBox _picker = new ComboBox();
public MyForm()
{
SuspendLayout();
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
_container.AutoSize = true;
_picker.Parent = _container;
_picker.SelectedIndexChanged += pickerOnSelect;
_address.Parent = _container;
_address.AutoSize = true;
Controls.Add(_container);
Load += mainFormLoader;
ResumeLayout();
}
private void mainFormLoader(object sender, EventArgs e)
{
string connectionString = @"Server=.\SQLExpress;Database=Test;Trusted_Connection=Yes;";
string queryString = "select name, address from MyTable";
using (SqlConnection connection = new SqlConnection(connectionString)) {
using (SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection)) {
DataTable table = new DataTable();
connection.Open();
adapter.Fill(table);
_picker.DataSource = table;
_picker.DisplayMember = "name";
_picker.ValueMember = "address";
pickerOnSelect(_picker, null);
}
}
}
private void pickerOnSelect(object sender, EventArgs e)
{
_address.Text = _picker.SelectedValue.ToString();
}
}
} Or if you want to do it manually:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace InannaRocks {
public class MyProgram {
public static void Main()
{
Application.Run(new MyForm());
}
}
class MyForm: Form {
private readonly string connectionString =
@"Server=.\SQLExpress;Database=Test;Trusted_Connection=Yes;";
private FlowLayoutPanel _container = new FlowLayoutPanel();
private TextBox _address = new TextBox();
private ComboBox _picker = new ComboBox();
public MyForm()
{
SuspendLayout();
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
_container.AutoSize = true;
_picker.Parent = _container;
_picker.SelectedIndexChanged += pickerOnSelect;
_address.Parent = _container;
_address.AutoSize = true;
Controls.Add(_container);
Load += mainFormLoader;
ResumeLayout();
}
private void mainFormLoader(object sender, EventArgs e)
{
string queryString = "select name from MyTable";
using (SqlConnection connection = new SqlConnection(connectionString)) {
using (SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection)) {
DataTable table = new DataTable();
connection.Open();
adapter.Fill(table);
foreach (DataRow dbRow in table.Rows)
_picker.Items.Add(dbRow["name"].ToString());
_picker.SelectedIndex = 0;
}
}
}
private void pickerOnSelect(object sender, EventArgs e)
{
string queryString =
"select address from MyTable where name = \'" +
_picker.Text + "\'";
using (SqlConnection connection = new SqlConnection(connectionString)) {
using (SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection)) {
DataTable table = new DataTable();
connection.Open();
adapter.Fill(table);
_address.ResetText();
foreach (DataRow dbRow in table.Rows)
_address.AppendText(dbRow["address"].ToString() + "; ");
}
}
}
}
}