You are on page 1of 3

190030392

D. ROOPA SREE

LAB-6

LAB-6: (Applying ADO.Net concepts)


Implement an ADO.Net application using Windows Forms to retrieve data
from Employee table and display all the rows in DataGridView Control.
Hint: Here you create a Windows Form by placing a DataGridview
Control and call the getEmp() from Page_load() method. Here getEmp() is
a method which is available in a separate class which will access the DB to
get the Emp Details.

A)

1. private void Form1_Load(object sender, EventArgs e) {


2. SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student"
, "server = MCNDESKTOP33; database = Avinash; UID = sa; password
= *******");
3. DataSet ds = new DataSet();
4. da.Fill(ds, "Student");
5. dataGridView1.DataSource = ds.Tables["Student"].DefaultView;
6. }

Code for the Save and Reset button

1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Linq;
7. using System.Text;
8. using System.Threading.Tasks;
9. using System.Windows.Forms;
10. using System.Data.SqlClient;
11.
12. namespace WindowsFormsDataGrid {
13.
14. public partial class Form1: Form {
15.
16. public Form1() {
17. InitializeComponent();
18. }
19.
20. private void Form1_Load(object sender, EventArgs e) {
21. SqlDataAdapter da = new SqlDataAdapter("SELECT * FR
OM Student", "server = MCNDESKTOP33; database = Avinash; UID = s
a; password = *******");
22. DataSet ds = new DataSet();
23. da.Fill(ds, "Student");
24. dataGridView1.DataSource = ds.Tables["Student"].Def
aultView;
25. }
26.
27. private void button1_Click(object sender, EventArgs e)
{
28. SqlConnection con = new SqlConnection("server = MCN
DESKTOP33; database = Avinash; UID = sa; password = *******");
29. con.Open();
30. string qur = "INSERT INTO Student VALUES ('" + text
Box1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','
" + textBox4.Text + "')";
31.
32. SqlCommand cmd = new SqlCommand(qur, con);
33. cmd.ExecuteNonQuery();
34. con.Close();
35. MessageBox.Show("Inserted sucessfully");
36. textBox1.Text = "";
37. textBox2.Text = "";
38. textBox3.Text = "";
39. textBox4.Text = "";
40. }
41.
42. private void button2_Click(object sender, EventArgs e)
{
43. textBox1.Text = "";
44. textBox2.Text = "";
45. textBox3.Text = "";
46. textBox4.Text = "";
47.
48. }
49. }
50. }

You might also like