Guest User

Untitled

a guest
Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. UIElements(); //calls UIElement function to describe the UI elements
  2. btnFilter.Visible = false; //disables search button on the header row
  3. enableEdit.Enabled = true;
  4. GridViewRow gvrow = TraineeGrid.SelectedRow;
  5. DataTable dataTable = new DataTable();
  6. var autoID = TraineeGrid.DataKeys[gvrow.RowIndex].Value;
  7. //Gets the row that fired the update command
  8. GridViewRow row = TraineeGrid.Rows[gvrow.RowIndex] as GridViewRow;
  9. TextBox tDname = gvrow.FindControl("txtDelegate") as TextBox;
  10. TextBox tRankPos = gvrow.FindControl("txtRankPos") as TextBox;
  11. TextBox tCompany = gvrow.FindControl("txtCompany") as TextBox;
  12. TextBox tCourse = gvrow.FindControl("txtCourseTitle") as TextBox;
  13. TextBox tCenter = gvrow.FindControl("txtTrainingCenter") as TextBox;
  14. TextBox tInstructor = gvrow.FindControl("txtInstructor") as TextBox;
  15. TextBox tSDate = gvrow.FindControl("txtStartDate") as TextBox;
  16. TextBox tEDate = gvrow.FindControl("txtEndDate") as TextBox;
  17. DropDownList dCertIssued = gvrow.FindControl("listCertIssued") as DropDownList;
  18. TextBox tCertNumber = gvrow.FindControl("txtCertNumber") as TextBox;
  19. using (connection)
  20. {
  21. string path = "D:\UpdateSQL.txt";
  22. StringBuilder sb = new StringBuilder();
  23. using (StreamReader sr = new StreamReader(path))
  24. {
  25. while (sr.Peek() >= 0)
  26. {
  27. sb.Append(sr.ReadLine());
  28. }
  29. //SQL Update Command
  30. string sql = sb.ToString();
  31.  
  32. if (tDname.Text != String.Empty && tRankPos.Text != String.Empty && tCompany.Text != String.Empty
  33. && tCourse.Text != String.Empty && tCenter.Text != String.Empty && tInstructor.Text != String.Empty
  34. && tSDate.Text != String.Empty && tEDate.Text != String.Empty && dCertIssued.SelectedIndex != 0
  35. && tCertNumber.Text != String.Empty)
  36. {
  37. DateTime sdt, edt;
  38. //checks if the date typed is valid
  39. if (DateTime.TryParseExact(tSDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out sdt) &&
  40. DateTime.TryParseExact(tEDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out edt))
  41. {
  42. //checks if startdate(sdt) is earlier than enddate(edt)
  43. if (sdt <= edt)
  44. {
  45. using (SqlCommand cmd = new SqlCommand(sql, connection))
  46. {
  47. //add values to update command
  48. cmd.Parameters.AddWithValue("@delegate", tDname.Text.Trim());
  49. cmd.Parameters.AddWithValue("@rankpos", tRankPos.Text.Trim());
  50. cmd.Parameters.AddWithValue("@company", tCompany.Text.Trim());
  51. cmd.Parameters.AddWithValue("@course", tCourse.Text.Trim());
  52. cmd.Parameters.AddWithValue("@center", tCenter.Text.Trim());
  53. cmd.Parameters.AddWithValue("@instructor", tInstructor.Text.Trim());
  54. cmd.Parameters.AddWithValue("@sDate", tSDate.Text.Trim());
  55. cmd.Parameters.AddWithValue("@eDate", tEDate.Text.Trim());
  56. cmd.Parameters.AddWithValue("@certIssued", dCertIssued.SelectedValue);
  57. cmd.Parameters.AddWithValue("@certNum", tCertNumber.Text.Trim());
  58. cmd.Parameters.AddWithValue("@AutoId", autoID);
  59.  
  60. connection.Open();
  61. cmd.ExecuteNonQuery();
  62. connection.Close();
  63. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  64. {
  65. da.Fill(dataTable);
  66. }
  67. //Disable edit mode after update
  68. TraineeGrid.EditIndex = -1;
  69. }
  70. }
  71. else
  72. ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Starting Date should be earlier than Ending Date')", true);
  73. }
  74. else
  75. ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter a valid date')", true);
  76. }
  77. else
  78. ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please fill up all the textfields')", true);
  79. }
  80. }
  81. //I'm checking if the ViewState is not null.
  82. //It determines if the data displayed in the gridivew is a searched data
  83. if (ViewState["sql"] != null) //this ViewState came from the Search function (btnFilterFname)
  84. {
  85. using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestCS"].ConnectionString))
  86. {
  87. //Get the sql command from viewstate and save it to datatable
  88. string sql2 = (string)ViewState["sql"];
  89. using (SqlCommand cmd = new SqlCommand(sql2, con))
  90. {
  91. using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
  92. {
  93. dataAdapter.Fill(dataTable);
  94. }
  95. }
  96. }
  97. //rebind GridView with the Searched Data we edited
  98. TraineeGrid.DataSource = dataTable;
  99. TraineeGrid.DataBind();
  100. }
  101. else
  102. PopulateData();
Add Comment
Please, Sign In to add comment