Retrieval of Data from Sql Data Base …

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()

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;

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


// Closed  reader then connection, to released the memory hold by these objects
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

// open conection Because it was closed earlier
// 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);




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s