1
Reply

PLEASE check It on code // I Can't Understand This Code //

Palash Kayal

Palash Kayal

Sep 5 2017 5:11 AM
204
  1. using System;  
  2. using System.Data;  
  3. using System.Windows.Forms;  
  4. using System.Data.SqlClient;  
  5. namespace InsertUpdateDeleteDemo  
  6. {  
  7. public partial class frmMain : Form  
  8. {  
  9. SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");  
  10. SqlCommand cmd;  
  11. SqlDataAdapter adapt;  
  12. //ID variable used in Updating and Deleting Record  
  13. int ID = 0;  
  14. public frmMain()  
  15. {  
  16.    //I Can't Understand This Code //  
  17.   
  18. InitializeComponent();  
  19. DisplayData();  
  20.   
  21.    //end Of I Can't Understand This Code //  
  22. }  
  23. //Insert Data  
  24. private void btn_Insert_Click(object sender, EventArgs e)  
  25. {  
  26. if (txt_Name.Text != "" && txt_State.Text != "")  
  27. {  
  28. cmd = new SqlCommand("insert into tbl_Record(Name,State) values(@name,@state)", con);  
  29. con.Open();  
  30. cmd.Parameters.AddWithValue("@name", txt_Name.Text);  
  31. cmd.Parameters.AddWithValue("@state", txt_State.Text);  
  32. cmd.ExecuteNonQuery();  
  33. con.Close();  
  34. MessageBox.Show("Record Inserted Successfully");  
  35. DisplayData();  
  36. ClearData();  
  37. }  
  38. else  
  39. {  
  40. MessageBox.Show("Please Provide Details!");  
  41. }  
  42. }  
  43. //Display Data in DataGridView  
  44. private void DisplayData()  
  45. {  
  46. con.Open();  
  47. DataTable dt=new DataTable();  
  48. adapt=new SqlDataAdapter("select * from tbl_Record",con);  
  49. adapt.Fill(dt);  
  50. dataGridView1.DataSource = dt;  
  51. con.Close();  
  52. }  
  53. //Clear Data  
  54. private void ClearData()  
  55. {  
  56. txt_Name.Text = "";  
  57. txt_State.Text = "";  
  58. ID = 0;  
  59. }  
  60. //dataGridView1 RowHeaderMouseClick Event  
  61. private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)  
  62. {  
  63. ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());  
  64. txt_Name.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();  
  65. txt_State.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();  
  66. }  
  67. //Update Record  
  68. private void btn_Update_Click(object sender, EventArgs e)  
  69. {  
  70. if (txt_Name.Text != "" && txt_State.Text != "")  
  71. {  
  72. cmd = new SqlCommand("update tbl_Record set Name=@name,State=@state where ID=@id", con);  
  73. con.Open();  
  74. cmd.Parameters.AddWithValue("@id", ID);  
  75. cmd.Parameters.AddWithValue("@name", txt_Name.Text);  
  76. cmd.Parameters.AddWithValue("@state", txt_State.Text);  
  77. cmd.ExecuteNonQuery();  
  78. MessageBox.Show("Record Updated Successfully");  
  79. con.Close();  
  80. DisplayData();  
  81. ClearData();  
  82. }  
  83. else  
  84. {  
  85. MessageBox.Show("Please Select Record to Update");  
  86. }  
  87. }  
  88. //Delete Record  
  89. private void btn_Delete_Click(object sender, EventArgs e)  
  90. {  
  91. if(ID!=0)  
  92. {  
  93. cmd = new SqlCommand("delete tbl_Record where ID=@id",con);  
  94. con.Open();  
  95. cmd.Parameters.AddWithValue("@id",ID);  
  96. cmd.ExecuteNonQuery();  
  97. con.Close();  
  98. MessageBox.Show("Record Deleted Successfully!");  
  99. DisplayData();  
  100. ClearData();  
  101. }  
  102. else  
  103. {  
  104. MessageBox.Show("Please Select Record to Delete");  
  105. }  
  106. }  
  107. }  
  108. }  

Answers (1)