Sunteți pe pagina 1din 31

ADO .

NET Examples

Connected Environment

Write a program for “Employee Detail Management System”. His Program


contains 4 modules.

1- Add Employee Detail


2- Delete Employee Detail
3- Modify Employee Detail
4- Display Employee List
5- Exit

Database Name : SP
Table Name : Emp

Field Datatype Size

Emp_id int --
Emp_Name varchar 50
Salary int --

Data.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace DataBase
{
class Data
{
SqlConnection con;
SqlCommand cmd;

public void Connect()


{
con = new
SqlConnection("server=.;database=SP;trusted_connection=true"
);
con.Open();
}

public void DisConnect()


{
con.Close();
}
public void Insert(int id, string name, int sal)
{
string s = "insert into emp values(" + id + ",'"
+ name + "'," + sal + ")";
cmd = new SqlCommand(s, con);
cmd.ExecuteNonQuery();

public SqlDataReader GetID()


{
cmd = new SqlCommand("select emp_id from emp",
con);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}

public void Delete(int id)


{
string s = "delete from emp where emp_id=" + id;
cmd = new SqlCommand(s, con);
cmd.ExecuteNonQuery();
}

public SqlDataReader GetData()


{
cmd = new SqlCommand("select * from emp", con);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}

public SqlDataReader GetDetail(int id)


{
string s = "select emp_name,salary from emp
where emp_id=" + id;
cmd = new SqlCommand(s, con);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}

public void Update(int id, string name, int sal)


{
string s = "update emp set emp_name='" + name +
"',salary=" + sal + " where emp_id=" + id;
cmd = new SqlCommand(s, con);
cmd.ExecuteNonQuery();
}
}
}
Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataBase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void addRecordToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Add f = new frm_Add();
f.Show();
}

private void deleteRecordToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Delete f = new frm_Delete();
f.Show();
}

private void displayRecordsToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Display f = new frm_Display();
f.Show();
}

rivate void modifyRecordToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Modify f = new frm_Modify();
f.Show();
}

}
}

frm_Add.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataBase
{
public partial class frm_Add : Form
{
public frm_Add()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int id = int.Parse(textBox1.Text);
string name = textBox2.Text;
int sal = int.Parse(textBox3.Text);

Data db = new Data();


db.Connect();
db.Insert(id, name, sal);
db.DisConnect();
MessageBox.Show("Record Inserted");
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
this.Hide();
}
}
}

frm_Delete.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataBase
{
public partial class frm_Delete : Form
{
public frm_Delete()
{
InitializeComponent();
}
private void frm_Delete_Load(object sender, EventArgs e)
{
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetID();
while (dr.Read())
{
comboBox1.Items.Add(dr[0].ToString());
}
db.DisConnect();
}

private void button1_Click(object sender, EventArgs e)


{
int id = int.Parse(comboBox1.Text);
Data db = new Data();
db.Connect();
db.Delete(id);
db.DisConnect();
MessageBox.Show("Record Deleted");
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
this.Hide();
}
}
}

frm_Modify.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataBase
{
public partial class frm_Modify : Form
{
public frm_Modify()
{
InitializeComponent();
}

private void frm_Modify_Load(object sender, EventArgs e)


{
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetID();
while (dr.Read())
{
comboBox1.Items.Add(dr[0].ToString());
}
db.DisConnect();
}

private void comboBox1_SelectedIndexChanged(object


sender, EventArgs e)
{
int id = int.Parse(comboBox1.Text);
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetDetail(id);
dr.Read();
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
db.DisConnect();
}

private void button1_Click(object sender, EventArgs e)


{
int id = int.Parse(comboBox1.Text);
string name = textBox1.Text;
int sal = int.Parse(textBox2.Text);
Data db = new Data();
db.Connect();
db.Update(id, name, sal);
db.DisConnect();
MessageBox.Show("Record Updated");
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
this.Hide();
}
}
}
frm_Display.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataBase
{
public partial class frm_Display : Form
{
static int id;
public frm_Display()
{
InitializeComponent();
}

private void frm_Display_Load(object sender, EventArgs e)


{
ShowData();
}

private void ShowData()


{
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetData();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{
id =
Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Val
ue);

private void button2_Click(object sender, EventArgs e)


{
Data db = new Data();
db.Connect();
db.Delete(id);
db.DisConnect();
ShowData();
}
}
}

DataBase Connectivity using Stored procedure


Example of Above Program

Stored Procedures

To Add Record in Emp Table

Create proc Add_Record(@eid int,@ename varchar(50), @esal


int)
As
Insert into emp values(@eid,@ename,@esal)

To Get All Records from Emp Table

Create Proc Display_Record


As
Select * from emp

To Get Next Employee ID

Create proc GetNextID(@eid int output)


As
declare @c int
select @c=count(*) from emp

if(@c=0)
select @eid=100
else
begin
select @c=max(emp_id) from emp
select @eid=@c+1
end

To Delete Record from table

Create proc Delete_Record(@eid int)


As
delete from emp where emp_id=@eid

To Modify Detail of Employee

Create proc Modify_Record(@eid int,@ename varchar(50),@esal


int)
As
Update emp set emp_name=@ename,salary=@esal where
emp_id=@eid

To Get Employee Name and Salary by Employee ID

Create proc GetDetail(@eid int)


As
select emp_name,salary from emp where emp_id=@eid

To Get All Employee ID

Create proc GetID


As
select emp_id from emp

Database.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace database_conn
{
class Database
{
SqlConnection con;
SqlCommand cmd;

public void Connect()


{
con = new SqlConnection
("server=SPinfo;database=nce; trusted_connection=true");
con.Open();
}

public void DisConnect()


{
con.Close();
}

public void Insert(int eid, string ename, int esal)


{
cmd = new SqlCommand("Add_Record", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid",
SqlDbType.Int).Value = eid;
cmd.Parameters.AddWithValue("@ename",
SqlDbType.VarChar).Value = ename;
cmd.Parameters.AddWithValue("@esal",
SqlDbType.Int).Value = esal;
cmd.ExecuteNonQuery();
}

public SqlDataReader GetID()


{
cmd = new SqlCommand("GetID", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}

public void Delete(int eid)


{
cmd = new SqlCommand("Delete_Record", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid",
SqlDbType.Int).Value = eid;
cmd.ExecuteNonQuery();
}

public SqlDataReader GetDetail(int eid)


{
cmd = new SqlCommand("GetDetail", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid",
SqlDbType.Int).Value = eid;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}

public void Modify(int eid, string ename, int esal)


{
cmd = new SqlCommand("Modify_Record", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid",
SqlDbType.Int).Value = eid;
cmd.Parameters.AddWithValue("@ename",
SqlDbType.VarChar).Value = ename;
cmd.Parameters.AddWithValue("@esal",
SqlDbType.Int).Value = esal;
cmd.ExecuteNonQuery();
}

public SqlDataReader GetData()


{
cmd = new SqlCommand("Display_Record", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}

public int GetNextID()


{
cmd = new SqlCommand("GetNextID", con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid",SqlDbType.In
t).Direction=ParameterDirection.Output;
cmd.ExecuteNonQuery();

int
id=Convert.ToInt32(cmd.Parameters["@eid"].Value);
return id;
}
}
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataBase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void addRecordToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Add f = new frm_Add();
f.Show();
}

private void deleteRecordToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Delete f = new frm_Delete();
f.Show();
}

private void displayRecordsToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Display f = new frm_Display();
f.Show();
}

rivate void modifyRecordToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Modify f = new frm_Modify();
f.Show();
}

}
}

frm_Add.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataBase
{
public partial class frm_Add : Form
{
public frm_Add()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
int id = int.Parse(textBox1.Text);
string name = textBox2.Text;
int sal = int.Parse(textBox3.Text);

Data db = new Data();


db.Connect();
db.Insert(id, name, sal);
db.DisConnect();
MessageBox.Show("Record Inserted");
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
}
}
}

frm_Delete.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataBase
{
public partial class frm_Delete : Form
{
public frm_Delete()
{
InitializeComponent();
}

private void frm_Delete_Load(object sender, EventArgs e)


{
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetID();
while (dr.Read())
{
comboBox1.Items.Add(dr[0].ToString());
}
db.DisConnect();
}
private void button1_Click(object sender, EventArgs e)
{
int id = int.Parse(comboBox1.Text);
Data db = new Data();
db.Connect();
db.Delete(id);
db.DisConnect();
MessageBox.Show("Record Deleted");
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
this.Hide();
}
}
}

frm_Modify.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataBase
{
public partial class frm_Modify : Form
{
public frm_Modify()
{
InitializeComponent();
}

private void frm_Modify_Load(object sender, EventArgs e)


{
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetID();
while (dr.Read())
{
comboBox1.Items.Add(dr[0].ToString());
}
db.DisConnect();
}

private void comboBox1_SelectedIndexChanged(object


sender, EventArgs e)
{
int id = int.Parse(comboBox1.Text);
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetDetail(id);
dr.Read();
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
db.DisConnect();
}

private void button1_Click(object sender, EventArgs e)


{
int id = int.Parse(comboBox1.Text);
string name = textBox1.Text;
int sal = int.Parse(textBox2.Text);
Data db = new Data();
db.Connect();
db.Update(id, name, sal);
db.DisConnect();
MessageBox.Show("Record Updated");
this.Hide();
}

private void button2_Click(object sender, EventArgs e)


{
this.Hide();
}
}
}
frm_Display.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataBase
{
public partial class frm_Display : Form
{
static int id;
public frm_Display()
{
InitializeComponent();
}

private void frm_Display_Load(object sender, EventArgs e)


{
ShowData();
}

private void ShowData()


{
Data db = new Data();
db.Connect();
SqlDataReader dr = db.GetData();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{
id =
Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Val
ue);

private void button2_Click(object sender, EventArgs e)


{
Data db = new Data();
db.Connect();
db.Delete(id);
db.DisConnect();
ShowData();
}
}
}

DisConnected Environment
Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DisConnected
{
public partial class Form1 : Form
{
public static SqlConnection con;
public static SqlDataAdapter da;
public static DataSet ds;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
con = new
SqlConnection("server=.;database=SP;trusted_connection=true"
);
da = new SqlDataAdapter("select * from emp",
con);
ds = new DataSet();
da.Fill(ds, "T1");
}

private void
displayRecordsToolStripMenuItem_Click(object sender,
EventArgs e)
{
frm_Display f = new frm_Display();
f.Show();
}

private void addRecordToolStripMenuItem_Click(object


sender, EventArgs e)
{
frm_Add f = new frm_Add();
f.Show();
}

private void
deleteRecordToolStripMenuItem_Click(object sender, EventArgs
e)
{
frm_Delete f = new frm_Delete();
f.Show();
}

private void
modifyRecordToolStripMenuItem_Click(object sender, EventArgs
e)
{
frm_Modify_ f = new frm_Modify_();
f.Show();
}
private void
finalUpdationToolStripMenuItem_Click(object sender,
EventArgs e)
{
SqlCommandBuilder sb = new
SqlCommandBuilder(Form1.da);
Form1.da.Update(Form1.ds,"T1");
MessageBox.Show("Data Updated At Server Side");

}
}
}

frm_Add.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DisConnected
{
public partial class frm_Add : Form
{
public frm_Add()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
DataRow dr = Form1.ds.Tables["T1"].NewRow();
dr[0] = int.Parse(textBox1.Text);
dr[1] = textBox2.Text;
dr[2] = int.Parse(textBox3.Text);
Form1.ds.Tables["T1"].Rows.Add(dr);
MessageBox.Show("Inserted");
this.Hide();
}
}
}

frm_Delete.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DisConnected
{
public partial class frm_Delete : Form
{
public frm_Delete()
{
InitializeComponent();
}

private void frm_Delete_Load(object sender, EventArgs e)


{
int n = Form1.ds.Tables["T1"].Rows.Count;
for (int i = 0; i < n; i++)
{
comboBox1.Items.Add(Form1.ds.Tables["T1"].R
ows[i][0].ToString());
}
}

private void button1_Click(object sender, EventArgs e)


{
int id = int.Parse(comboBox1.Text);
DataRow[] dr =
Form1.ds.Tables["T1"].Select("emp_id=" + id);
dr[0].Delete();
MessageBox.Show("Deleted");
this.Hide();
}
}
}

frm_Modify.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DisConnected
{
public partial class frm_Modify_ : Form
{
public frm_Modify_()
{
InitializeComponent();
}

private void frm_Modify__Load(object sender, EventArgs e)


{
int n = Form1.ds.Tables["T1"].Rows.Count;
for (int i = 0; i < n; i++)
{
comboBox1.Items.Add(Form1.ds.Tables["T1"].R
ows[i][0].ToString());
}
}
private void comboBox1_SelectedIndexChanged(object
sender, EventArgs e)
{
int id = int.Parse(comboBox1.Text);
DataRow[] dr =
Form1.ds.Tables["T1"].Select("emp_id=" + id);
textBox1.Text = dr[0][1].ToString();
textBox2.Text = dr[0][2].ToString();
}

private void button1_Click(object sender, EventArgs e)


{
int id = int.Parse(comboBox1.Text);
DataRow[] dr =
Form1.ds.Tables["T1"].Select("emp_id=" + id);
dr[0][1] = textBox1.Text;
dr[0][2] = int.Parse(textBox2.Text);
MessageBox.Show("Record UpdateD");
this.Hide();
}
}
}

frm_Display.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DisConnected
{
public partial class frm_Display : Form
{
public frm_Display()
{
InitializeComponent();
}

private void frm_Display_Load(object sender, EventArgs e)


{
dataGridView1.DataSource = Form1.ds.Tables["T1"];
}

private void button1_Click(object sender, EventArgs e)


{
this.Hide();
}
}
}

Data Table at Run Time

Database Name : SP
Table Name : Emp

Field Datatype Size

Emp_id int --
Emp_Name varchar 50
Salary int --

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
DataTable dt;
static int Emp_id;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
DisableControl();

// Create Table and Columns

dt = new DataTable("Emp");
DataColumn dc1 = new DataColumn("eid", typeof(int));
DataColumn dc2 = new DataColumn("ename", typeof(string));
DataColumn dc3 = new DataColumn("salary", typeof(int));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);

// Define Primary Key in Data Table

DataColumn[] pk = new DataColumn[1];


pk[0] = dt.Columns[0];
dt.PrimaryKey = pk;

private void ShowDatainGrid()


{
dataGridView1.DataSource = dt;
}

private void DisableControl()


{
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;

}
private void EnableControl()
{
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox1.Focus();

private void ClearControl()


{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
EnableControl();
ClearControl();
}

private void button2_Click(object sender, EventArgs e)


{
DataRow dr = dt.NewRow();
dr[0] = int.Parse(textBox1.Text);
dr[1] = textBox2.Text;
dr[2] = int.Parse(textBox3.Text);
dt.Rows.Add(dr);
ShowDatainGrid();
DisableControl();
}

private void dataGridView1_CellClick(object sender,


DataGridViewCellEventArgs e)
{
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
Emp_id
=Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Va
lue);

DataRow[] dr = dt.Select("eid=" + Emp_id);


textBox1.Text = dr[0][0].ToString();
textBox2.Text = dr[0][1].ToString();
textBox3.Text = dr[0][2].ToString();
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
}

private void button3_Click(object sender, EventArgs e)


{
DataRow[] dr = dt.Select("eid=" + Emp_id);
dr[0].Delete();
ShowDatainGrid();
DisableControl();
}

private void button4_Click(object sender, EventArgs e)


{
DataRow[] dr = dt.Select("eid=" + Emp_id);
dr[0][0] = int.Parse(textBox1.Text);
dr[0][1] = textBox2.Text;
dr[0][2] = int.Parse(textBox3.Text);
ShowDatainGrid();
DisableControl();
}
private void button5_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection
("server=dotnet2005;database=SP;trusted_connection=true");
SqlDataAdapter da = new SqlDataAdapter("select * from emp",
con);
DataSet ds = new DataSet();
da.Fill(ds, "T1");

foreach (DataRow dr in dt.Rows)


{
DataRow dd = ds.Tables[0].NewRow();
dd[0] = dr[0];
dd[1] = dr[1];
dd[2] = dr[2];
ds.Tables[0].Rows.Add(dd);
}

SqlCommandBuilder sb = new SqlCommandBuilder(da);


da.Update(ds,"T1");
MessageBox.Show("Records Saved in Database");
DisableControl();
dataGridView1.DataSource = null;
}

private void button6_Click(object sender, EventArgs e)


{
Application.Exit();
}

}
}

Record Navigation Using DataTable


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
DataSet ds;
static int pos = 0;
static int rowcount = 0;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
SqlConnection con = new
SqlConnection("server=SPinfot;database=deepak;trusted_conne
ction=true");
SqlDataAdapter da = new SqlDataAdapter("select * from
emp", con);
ds = new DataSet();
da.Fill(ds, "T1");
rowcount = ds.Tables[0].Rows.Count;
MakeReadOnly();
ShowRecord();

}
private void MakeReadOnly()
{
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;

}
private void ShowRecord()
{

textBox1.Text = ds.Tables[0].Rows[pos][0].ToString();
textBox2.Text = ds.Tables[0].Rows[pos][1].ToString();
textBox3.Text = ds.Tables[0].Rows[pos][2].ToString();
}

private void button1_Click(object sender, EventArgs e)


{
pos = 0;
ShowRecord();
}

private void button4_Click(object sender, EventArgs e)


{
pos = rowcount - 1;
ShowRecord();
}

private void button2_Click(object sender, EventArgs e)


{
if (pos == 0)
pos = rowcount - 1;
else
pos--;

ShowRecord();
}

private void button3_Click(object sender, EventArgs e)


{
if (pos == rowcount - 1)
pos = 0;
else
pos++;

ShowRecord();
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}

S-ar putea să vă placă și