MAKING PROGRAMING CODE BY GAUTAM RANA
-
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 TestProgram { public partial class StudentDetails : Form { SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\TestProgram\TestProgram\Student.mdf;Integrated Security=True;User Instance=True"); public StudentDetails() { InitializeComponent(); } protected void OpenConnection() { if (con.State == System.Data.ConnectionState.Open) { con.Close(); } con.Open(); } protected void CloseConnection() { con.Close(); } protected void Clear() { txtname.Text = ""; txtaddress.Text = ""; txtcourse.Text = ""; txtaddress.Text = ""; // btndelete.Enabled = false; } protected void bindGrid() { try { SqlCommand cmd = new SqlCommand("select * from Student_Detail", con); OpenConnection(); SqlDataReader dr = cmd.ExecuteReader(); dataGridView1.Rows.Clear(); while (dr.Read() == true) { dataGridView1.Rows.Add(dr[0], dr[1], dr[2], dr[3]); } CloseConnection(); } catch (Exception err) { MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnsave_Click(object sender, EventArgs e) { try { if (txtname.Text == "") { MessageBox.Show("Please enter details ", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtname.Focus(); return; } SqlCommand cmd = new SqlCommand("SP_Insert", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter parm = new SqlParameter("@Student_Name", SqlDbType.VarChar); parm.Value = txtname.Text; parm.Direction = ParameterDirection.Input; cmd.Parameters.Add(parm); parm = new SqlParameter("@Student_Course", SqlDbType.VarChar); parm.Value = txtcourse.Text; parm.Direction = ParameterDirection.Input; cmd.Parameters.Add(parm); parm = new SqlParameter("@Student_Address", SqlDbType.VarChar); parm.Value = txtaddress.Text; parm.Direction = ParameterDirection.Input; cmd.Parameters.Add(parm); OpenConnection(); cmd.ExecuteNonQuery(); MessageBox.Show("Data Save Successfully"); Clear(); bindGrid(); CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } private void btnupdate_Click(object sender, EventArgs e) { try { int id; id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()); SqlCommand cmd = new SqlCommand("SP_Update", con); cmd.C