using System;
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 WindowsFormsApplication17
{
public partial class Form1 : Form
{
public OleDbConnection conn = new OleDbConnection();
public Form1()
{
InitializeComponent();
string s = “provider= Microsoft.jet.oledb.4.0; data source= E:\\UIT\\Visual Programming CS201 by Syed Faisal Ali\\Week 7\\Nwind.mdb”;
conn.ConnectionString = s;
conn.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
//Assign SQL statement to the command object
cmd.CommandText = “select LastName from Employees”;
// Define the type of execution , TEXT means SQL Statement
cmd.CommandType = CommandType.Text;
// Assign active open connection to the command, so that the command will be executed on a particular connection
cmd.Connection = conn;
// Reader is used to hold data that is retured by the SQL query. The data is returned when the command is executed .
OleDbDataReader r = cmd.ExecuteReader();
// Loop until data is available in the Reader , one by one
while (r.Read())
{
// Add each record to the comboBox
comboBox1.Items.Add(r.GetString(0));
}
// Closed reader then connection, to released the memory hold by these objects
r.Close();
conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// open conection Because it was closed earlier
conn.Open();
// Define another Command object
OleDbCommand cmdS = new OleDbCommand();
// We want to select the name of the customers , who’s ID is been selected at the ComboBox by the user.
cmdS.CommandText = “select FirstName from Employees where LastName='” + comboBox1.Text + “‘”;
cmdS.CommandType = CommandType.Text;
cmdS.Connection = conn;
OleDbDataReader r1 = cmdS.ExecuteReader();
while (r1.Read())
{
// Fill the textbox by the data returned by the SQL Query against the Customer ID
textBox1.Text = r1.GetString(0);
}
r1.Close();
conn.Close();
}
}
}