Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UIElements(); //calls UIElement function to describe the UI elements
- btnFilter.Visible = false; //disables search button on the header row
- enableEdit.Enabled = true;
- GridViewRow gvrow = TraineeGrid.SelectedRow;
- DataTable dataTable = new DataTable();
- var autoID = TraineeGrid.DataKeys[gvrow.RowIndex].Value;
- //Gets the row that fired the update command
- GridViewRow row = TraineeGrid.Rows[gvrow.RowIndex] as GridViewRow;
- TextBox tDname = gvrow.FindControl("txtDelegate") as TextBox;
- TextBox tRankPos = gvrow.FindControl("txtRankPos") as TextBox;
- TextBox tCompany = gvrow.FindControl("txtCompany") as TextBox;
- TextBox tCourse = gvrow.FindControl("txtCourseTitle") as TextBox;
- TextBox tCenter = gvrow.FindControl("txtTrainingCenter") as TextBox;
- TextBox tInstructor = gvrow.FindControl("txtInstructor") as TextBox;
- TextBox tSDate = gvrow.FindControl("txtStartDate") as TextBox;
- TextBox tEDate = gvrow.FindControl("txtEndDate") as TextBox;
- DropDownList dCertIssued = gvrow.FindControl("listCertIssued") as DropDownList;
- TextBox tCertNumber = gvrow.FindControl("txtCertNumber") as TextBox;
- using (connection)
- {
- string path = "D:\UpdateSQL.txt";
- StringBuilder sb = new StringBuilder();
- using (StreamReader sr = new StreamReader(path))
- {
- while (sr.Peek() >= 0)
- {
- sb.Append(sr.ReadLine());
- }
- //SQL Update Command
- string sql = sb.ToString();
- if (tDname.Text != String.Empty && tRankPos.Text != String.Empty && tCompany.Text != String.Empty
- && tCourse.Text != String.Empty && tCenter.Text != String.Empty && tInstructor.Text != String.Empty
- && tSDate.Text != String.Empty && tEDate.Text != String.Empty && dCertIssued.SelectedIndex != 0
- && tCertNumber.Text != String.Empty)
- {
- DateTime sdt, edt;
- //checks if the date typed is valid
- if (DateTime.TryParseExact(tSDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out sdt) &&
- DateTime.TryParseExact(tEDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out edt))
- {
- //checks if startdate(sdt) is earlier than enddate(edt)
- if (sdt <= edt)
- {
- using (SqlCommand cmd = new SqlCommand(sql, connection))
- {
- //add values to update command
- cmd.Parameters.AddWithValue("@delegate", tDname.Text.Trim());
- cmd.Parameters.AddWithValue("@rankpos", tRankPos.Text.Trim());
- cmd.Parameters.AddWithValue("@company", tCompany.Text.Trim());
- cmd.Parameters.AddWithValue("@course", tCourse.Text.Trim());
- cmd.Parameters.AddWithValue("@center", tCenter.Text.Trim());
- cmd.Parameters.AddWithValue("@instructor", tInstructor.Text.Trim());
- cmd.Parameters.AddWithValue("@sDate", tSDate.Text.Trim());
- cmd.Parameters.AddWithValue("@eDate", tEDate.Text.Trim());
- cmd.Parameters.AddWithValue("@certIssued", dCertIssued.SelectedValue);
- cmd.Parameters.AddWithValue("@certNum", tCertNumber.Text.Trim());
- cmd.Parameters.AddWithValue("@AutoId", autoID);
- connection.Open();
- cmd.ExecuteNonQuery();
- connection.Close();
- using (SqlDataAdapter da = new SqlDataAdapter(cmd))
- {
- da.Fill(dataTable);
- }
- //Disable edit mode after update
- TraineeGrid.EditIndex = -1;
- }
- }
- else
- ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Starting Date should be earlier than Ending Date')", true);
- }
- else
- ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter a valid date')", true);
- }
- else
- ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please fill up all the textfields')", true);
- }
- }
- //I'm checking if the ViewState is not null.
- //It determines if the data displayed in the gridivew is a searched data
- if (ViewState["sql"] != null) //this ViewState came from the Search function (btnFilterFname)
- {
- using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestCS"].ConnectionString))
- {
- //Get the sql command from viewstate and save it to datatable
- string sql2 = (string)ViewState["sql"];
- using (SqlCommand cmd = new SqlCommand(sql2, con))
- {
- using (SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd))
- {
- dataAdapter.Fill(dataTable);
- }
- }
- }
- //rebind GridView with the Searched Data we edited
- TraineeGrid.DataSource = dataTable;
- TraineeGrid.DataBind();
- }
- else
- PopulateData();
Add Comment
Please, Sign In to add comment