Guest User

Untitled

a guest
Nov 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  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. namespace Student_Information
  12. {
  13. public partial class TeacherInformation : Form
  14. {
  15.  
  16. SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-VTPQ9MQSQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True");
  17. int Id = 0;
  18. SqlCommand cmd;
  19. SqlDataAdapter data;
  20. DataTable dt = new DataTable();
  21.  
  22.  
  23. public TeacherInformation()
  24. {
  25. InitializeComponent();
  26.  
  27.  
  28. }
  29.  
  30. private void newButton_Click(object sender, EventArgs e)
  31. {
  32. EnableDisable();
  33.  
  34. }
  35.  
  36. private void saveButton_Click(object sender, EventArgs e)
  37. {
  38. try
  39. {
  40. if (saveButton.Text == "Save")
  41. {
  42. cmd = new SqlCommand("AddTeacher", conn);
  43. cmd.CommandType = CommandType.StoredProcedure;
  44.  
  45. SqlParameter[] param = new SqlParameter[3];
  46.  
  47. param[0] = new SqlParameter("@TeacherName", SqlDbType.VarChar);
  48. param[0].Value = nameTextBox.Text.Trim();
  49.  
  50. param[1] = new SqlParameter("@Designation", SqlDbType.VarChar);
  51. param[1].Value = designationComboBox.SelectedItem.ToString();
  52.  
  53. param[2] = new SqlParameter("@JoiningDate", SqlDbType.Date);
  54. param[2].Value = joiningDateTimePicker.Text;
  55.  
  56. cmd.Parameters.AddRange(param);
  57. conn.Open();
  58. cmd.ExecuteNonQuery();
  59. conn.Close();
  60. MessageBox.Show("Insert Done", "Insert Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
  61.  
  62. }
  63.  
  64. else if (saveButton.Text =="Update")
  65. {
  66.  
  67. cmd = new SqlCommand("UpdateTeacher", conn);
  68. cmd.CommandType = CommandType.StoredProcedure;
  69.  
  70. SqlParameter[] param = new SqlParameter[4];
  71.  
  72. param[0] = new SqlParameter("@Id", SqlDbType.Int);
  73. param[0].Value = Convert.ToInt32(Id);
  74.  
  75. param[1] = new SqlParameter("@TeacherName", SqlDbType.VarChar);
  76. param[1].Value = nameTextBox.Text.Trim();
  77.  
  78. param[2] = new SqlParameter("@Designation", SqlDbType.VarChar);
  79. param[2].Value = designationComboBox.SelectedItem.ToString();
  80.  
  81. param[3] = new SqlParameter("@JoiningDate", SqlDbType.Date);
  82. param[3].Value = joiningDateTimePicker.Text;
  83.  
  84. cmd.Parameters.AddRange(param);
  85. conn.Open();
  86. cmd.ExecuteNonQuery();
  87. conn.Close();
  88. MessageBox.Show("Update Done", "Update Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
  89.  
  90. }
  91. else
  92. {
  93. MessageBox.Show("something is Wrong");
  94. }
  95.  
  96. Clear();
  97.  
  98. }
  99. catch (Exception ex)
  100. {
  101. MessageBox.Show(ex.Message);
  102. }
  103.  
  104. }
  105.  
  106. private void deleteButton_Click_1(object sender, EventArgs e)
  107. {
  108. try
  109. {
  110. cmd = new SqlCommand("DeleteTeacher", conn);
  111. cmd.CommandType = CommandType.StoredProcedure;
  112.  
  113. SqlParameter[] param = new SqlParameter[1];
  114.  
  115. param[0] = new SqlParameter("@Id", SqlDbType.Int);
  116. param[0].Value = Convert.ToInt32(Id);
  117.  
  118. cmd.Parameters.AddRange(param);
  119. conn.Open();
  120. cmd.ExecuteNonQuery();
  121. conn.Close();
  122. MessageBox.Show("Done", "Delete Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
  123. Clear();
  124. }
  125.  
  126. catch (Exception ex)
  127. {
  128. MessageBox.Show(ex.Message);
  129. }
  130.  
  131. }
  132.  
  133.  
  134. public void EnableDisable()
  135. {
  136. saveButton.Enabled = true;
  137. deleteButton.Enabled = true;
  138. nameTextBox.Enabled = true;
  139. designationComboBox.Enabled= true;
  140. joiningDateTimePicker.Enabled = true;
  141.  
  142. }
  143.  
  144. public void Reset()
  145. {
  146. saveButton.Enabled = false;
  147. deleteButton.Enabled = false;
  148. nameTextBox.Enabled = false;
  149. designationComboBox.Enabled = false;
  150. joiningDateTimePicker.Enabled = false;
  151. newButton.Enabled = true;
  152. }
  153.  
  154. private void Clear()
  155. {
  156. nameTextBox.Text = "";
  157. designationComboBox.SelectedIndex = -1;
  158. joiningDateTimePicker.Text = "";
  159. }
  160.  
  161. private void TeacherInformation_Load(object sender, EventArgs e)
  162. {
  163. SearchDataGridView();
  164. Clear();
  165.  
  166. saveButton.Enabled = false;
  167. deleteButton.Enabled = false;
  168. newButton.Enabled = true;
  169. nameTextBox.Enabled = false;
  170. designationComboBox.Enabled = false;
  171. joiningDateTimePicker.Enabled = false;
  172. }
  173.  
  174. public void SearchDataGridView()
  175. {
  176. if (conn.State == ConnectionState.Closed)
  177. conn.Open();
  178. SqlDataAdapter data = new SqlDataAdapter("SearchTeacherByName", conn);
  179. data.SelectCommand.CommandType = CommandType.StoredProcedure;
  180. data.SelectCommand.Parameters.AddWithValue("@TeacherName", searchTextBox.Text.Trim());
  181. DataTable dt = new DataTable();
  182. data.Fill(dt);
  183. teacherListView.View = View.Details;
  184. teacherListView.FullRowSelect = true;
  185.  
  186. foreach (DataRow row in dt.Rows)
  187. {
  188. ListViewItem listitem = new ListViewItem(row["Id"].ToString());
  189. listitem.SubItems.Add(row["TeacherName"].ToString());
  190. listitem.SubItems.Add(row["Designation"].ToString());
  191. listitem.SubItems.Add(row["JoiningDate"].ToString());
  192. teacherListView.Items.Add(listitem);
  193.  
  194. }
  195.  
  196. conn.Close();
  197. }
  198.  
  199. private void searchButton_Click(object sender, EventArgs e)
  200. {
  201. try
  202. {
  203. SearchDataGridView();
  204. }
  205. catch (Exception ex)
  206. {
  207. MessageBox.Show(ex.Message);
  208. }
  209. }
  210.  
  211. private void teacherListView_DoubleClick(object sender, EventArgs e)
  212. {
  213. {
  214. if (teacherListView.SelectedItems.Count > -1)
  215. // return;
  216.  
  217. Id = Convert.ToInt32(teacherListView.SelectedItems[0].SubItems[0].ToString());
  218. nameTextBox.Text =teacherListView.SelectedItems[0].SubItems[1].Text;
  219. designationComboBox.Text =teacherListView.SelectedItems[0].SubItems[2].Text;
  220. joiningDateTimePicker.Value =Convert.ToDateTime(teacherListView.SelectedItems[0].SubItems[3].Text.ToString());
  221. saveButton.Text = "Update";
  222. deleteButton.Enabled = true;
  223. }
  224.  
  225.  
  226. }
  227.  
  228. private void resetButton_Click(object sender, EventArgs e)
  229. {
  230. Reset();
  231. }
  232.  
  233.  
  234.  
  235.  
  236. }
  237. }
  238.  
  239.  
  240. [1]: https://i.stac
Add Comment
Please, Sign In to add comment