C# Lab Program 9

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

C# Lab Program 9

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.SqlClient;

namespace program9student
{
publicpartialclassForm1 : Form
{
SqlConnection con = newSqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FAIZ\Documents\Visual Studio
2008\Projects\Student_database\Student_database\Faiz.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True");

public Form1()
{
InitializeComponent();
}
DataTable dt = newDataTable();
int indexrow = 0;

privatevoid Form1_Load(object sender, EventArgs e)


{

disp_data();

privatevoid button1_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into student9 values('" + textBox1.Text
+ "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text +
"','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','"
+ textBox8.Text + "','" + textBox9.Text + "')";
cmd.ExecuteNonQuery();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
con.Close();
disp_data();
MessageBox.Show("record inserted sucessfully");

}
publicvoid disp_data()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from student9";
cmd.ExecuteNonQuery();
DataTable dt = newDataTable();
SqlDataAdapter da = newSqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();

privatevoid button2_Click(object sender, EventArgs e)


{
DataGridViewRow newDataRow=dataGridView1.Rows[indexrow];

newDataRow.Cells[0].Value = textBox1.Text;
newDataRow.Cells[1].Value = textBox2.Text;
newDataRow.Cells[2].Value = textBox3.Text;
newDataRow.Cells[3].Value = textBox4.Text;
newDataRow.Cells[4].Value = textBox5.Text;
newDataRow.Cells[5].Value = textBox6.Text;
newDataRow.Cells[6].Value = textBox7.Text;
newDataRow.Cells[7].Value = textBox8.Text;
newDataRow.Cells[8].Value = textBox9.Text;
MessageBox.Show("record updated sucessfully");

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";

privatevoid button3_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from student9 where
rollno='"+textBox2.Text+"'";
cmd.ExecuteNonQuery();
con.Close();
disp_data();
MessageBox.Show("record deleted sucessfully");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
}}}

You might also like