Lab 11 and 12 Objectives: Lab Task Object Oriented Programming (C++ and C#) NED University of Engineering and Technology
Lab 11 and 12 Objectives: Lab Task Object Oriented Programming (C++ and C#) NED University of Engineering and Technology
Lab 11 and 12
Objectives
Program 1
This simple application how to create database connectivity in C # application.
Steps
Create a Database consisting of four tables containing necessary
attributes of these tables along with the connectivity’s .
1. Department.
2. Teachers.
3. Student.
4. Subjects
5. Courses
6. Program
1
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology
da.Fill(ds);
‘DS is the name of Data Set
Do It Yourself: Code click events of “<” button and “>>” button yourself.
Program 2
This simple application demonstrates How to Use DataGridView control to
display data:
1. Open a new form and place DataGrid Control and a Button on your
form.
2. As in previous example import Oledb namespace and declare
Connection, Data Adapter and Data Set as global variables.
3. Go to Load Event of your for and enter the following coding:
2
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology
DataSet d1 = new DataSet("student");
con.Open();
adap.Fill(d1, "student");
dataGrid1.DataSource = d1;
Program 3:
Solution:
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
source=f:/std.mdb");
DataSet d1 = new DataSet("student");
int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = d1.Tables[0].Rows[0]["ID"].ToString();
textBox2.Text = d1.Tables[0].Rows[0]["Name"].ToString();
textBox3.Text = d1.Tables[0].Rows[0]["Phone"].ToString();
textBox4.Text = d1.Tables[0].Rows[0]["Address"].ToString();
3
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology
textBox5.Text = d1.Tables[0].Rows[0]["deptid"].ToString();
textBox1.Text = d1.Tables[0].Rows[counter]["ID"].ToString();
textBox2.Text = d1.Tables[0].Rows[counter]["Name"].ToString();
textBox3.Text = d1.Tables[0].Rows[counter]["Phone"].ToString();
textBox4.Text = d1.Tables[0].Rows[counter]["Address"].ToString();
textBox5.Text = d1.Tables[0].Rows[counter]["deptid"].ToString();
}
}
4
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology
if (counter < d1.Tables[0].Rows.Count - 1)
{
counter = d1.Tables[0].Rows.Count - 1;
textBox1.Text = d1.Tables[0].Rows[counter]["ID"].ToString();
textBox2.Text = d1.Tables[0].Rows[counter]["Name"].ToString();
textBox3.Text = d1.Tables[0].Rows[counter]["Phone"].ToString();
textBox4.Text = d1.Tables[0].Rows[counter]["Address"].ToString();
textBox5.Text = d1.Tables[0].Rows[counter]["deptid"].ToString();
}
}
adap2.Fill(d2,"emp");
dataGrid2.DataSource=d2;
5
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology
OleDbCommand com3 = new OleDbCommand("UPDATE student set
Address='Lahore' where ID= '" + textBox1.Text + " ' ", con);
com3.ExecuteNonQuery();
MessageBox.Show(" One record has been updated");
6
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology
Program 5:
Steps
7
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology
8
Lab Task
Object Oriented Programming (C++ and C#)
NED University of Engineering and Technology