You are on page 1of 4

using using using using using using using using using

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

namespace Connectivity { public partial class Form1 : Form { public Form1() { InitializeComponent(); } SqlConnection objConn = new SqlConnection("server=.\\sqlexpress;database=student;Integrated Security = True"); SqlDataAdapter objDA= new SqlDataAdapter(); DataSet objDS= new DataSet(); DataTable objDT= new DataTable(); Int32 CurRec, TotRec; public void LoadData() { objDA.SelectCommand = new SqlCommand();

objDA.SelectCommand.Connection = objConn; objDA.SelectCommand.CommandText = "select * from s"; objDA.SelectCommand.CommandType = CommandType.Text; objConn.Open(); objDA.SelectCommand.ExecuteNonQuery(); objDA.Fill(objDS, "StudentMaster"); objConn.Close(); objDT = objDS.Tables[0]; MessageBox.Show("Data loaded successfully"); TotRec = objDT.Rows.Count-1; } public void BindControls() { txtCity.Text = objDT.Rows[CurRec]["City"].ToString(); txtPhone.Text = objDT.Rows[CurRec] ["phone"].ToString(); txtProgCode.Text = objDT.Rows[CurRec] ["programcode"].ToString(); txtRegNo.Text = objDT.Rows[CurRec] ["regno"].ToString(); txtSName.Text = objDT.Rows[CurRec] ["sname"].ToString(); txtState.Text = objDT.Rows[CurRec] ["state"].ToString(); dataGridView1.DataSource = objDT; } private void Form1_Load(object sender, EventArgs e) { LoadData(); BindControls(); } private void btnAddNew_Click(object sender, EventArgs e) { txtCity.Text = ""; txtPhone.Text = ""; txtProgCode.Text = ""; txtRegNo.Text = ""; txtSName.Text = ""; txtState.Text = ""; dataGridView1.DataSource = null; txtSName.Focus(); } private void btnSave_Click(object sender, EventArgs e) { objDA.InsertCommand = new SqlCommand(); objDA.InsertCommand.Connection = objConn;

objDA.InsertCommand.CommandText = "insert into s (sname, regno, programcode, city, state, phone) values ('" + txtSName.Text + "','" + txtRegNo.Text + "','" + txtProgCode.Text + "','" + txtCity.Text + "','" + txtState.Text + "','" + txtPhone.Text + "')"; objDA.InsertCommand.CommandType = CommandType.Text; objConn.Open(); objDA.InsertCommand.ExecuteNonQuery(); objConn.Close(); MessageBox.Show("One Record Inserted Successfully"); objDS.Tables[0].Rows.Clear(); objDT.Rows.Clear(); dataGridView1.DataSource = null; LoadData(); } private void btnUpdate_Click(object sender, EventArgs e) { objDA.UpdateCommand = new SqlCommand(); objDA.UpdateCommand.Connection = objConn; objDA.UpdateCommand.CommandText = "update s set sname='" + txtSName.Text + "',programcode='" + txtProgCode.Text + "', city='" + txtCity.Text + "', state='" + txtState.Text + "', phone='" + txtPhone.Text + "' where regno= '" + txtRegNo.Text + "'"; objDA.UpdateCommand.CommandType = CommandType.Text; objConn.Open(); objDA.UpdateCommand.ExecuteNonQuery(); objConn.Close(); MessageBox.Show("One Record Updated Successfully"); objDS.Tables[0].Rows.Clear(); objDT.Rows.Clear(); dataGridView1.DataSource = null; LoadData(); } private void btnDelete_Click(object sender, EventArgs e) { objDA.DeleteCommand = new SqlCommand(); objDA.DeleteCommand.Connection = objConn; objDA.DeleteCommand.CommandText = "Delete from s where regno= '" + txtRegNo.Text + "'"; objDA.DeleteCommand.CommandType = CommandType.Text; objConn.Open(); objDA.DeleteCommand.ExecuteNonQuery(); objConn.Close(); MessageBox.Show("One Record Deleted Successfully"); objDS.Tables[0].Rows.Clear(); objDT.Rows.Clear(); dataGridView1.DataSource = null;

LoadData(); } private void btnFirst_Click(object sender, EventArgs e) { CurRec = 0; BindControls(); } private void btnPrevious_Click(object sender, EventArgs e) { CurRec--; if (CurRec >= 0) BindControls(); else MessageBox.Show("You are at first record"); } private void btnNext_Click(object sender, EventArgs e) { CurRec++; if (CurRec <= TotRec) BindControls(); else MessageBox.Show("You are at Last Record"); } private void btnLast_Click(object sender, EventArgs e) { CurRec = TotRec; BindControls(); }

You might also like