public static Systemtime GetSystemTime() { SqlConnection myconn = null; Systemtime st = null; try { myconn = new SqlConnection(); SqlCommand comm = new SqlCommand(); myconn.ConnectionString = connectionString; myconn.Open(); comm.Connection = myconn; comm.CommandText = "select StartDate, EndDate from SystemDate"; SqlDataReader dr = comm.ExecuteReader(); if (dr.Read()) { DateTime start = (DateTime)(dr["StartDate"]); DateTime end = (DateTime)(dr["EndDate"]); st = new Systemtime(start, end); } dr.Close(); } catch (SqlException) { return st; } finally { myconn.Close(); } return st; }
public static bool InsertSystemTime(Systemtime st) { SqlConnection myconn = null; bool result = false; try { myconn = new SqlConnection(); SqlCommand comm = new SqlCommand(); myconn.ConnectionString = connectionString; myconn.Open(); comm.Connection = myconn; comm.CommandText = "insert into SystemDate" + "(StartDate,EndDate) values" + "(@starttime, @endtime)"; comm.Parameters.AddWithValue("@starttime", st.Startdate); comm.Parameters.AddWithValue("@endtime", st.Enddate); int rowsAffected = comm.ExecuteNonQuery(); if (rowsAffected > 0) { result = true; } } catch (SqlException) { return result; } finally { myconn.Close(); } return result; }
protected void Confirmbtn_Click(object sender, EventArgs e) { if (Session["StartDate"] != null && Session["EndDate"] != null) { if (SelectStartTbx.Text != "" && SelectEndDateTbx.Text != "") { Systemtime snull = dbmanager.GetSystemTime(); if (snull == null) { if (Session["Invalid"] == null) { DateTime d1 = (DateTime)Session["StartDate"]; DateTime d2 = (DateTime)Session["EndDate"]; Systemtime st = new Systemtime(d1, d2); bool result = dbmanager.InsertSystemTime(st); if (result == true) { dbmanager.DeletePreviousAllSaved(); Messaglbl.Text = "Dates updated successfully."; Systemtime sttime = dbmanager.GetSystemTime(); if (sttime != null) { startlbl.Text = sttime.Startdate.ToLongDateString(); endlbl.Text = sttime.Enddate.ToLongDateString(); } else { startlbl.Text = "--"; endlbl.Text = "--"; } } else { Messaglbl.Text = "Fail to update date."; } } } else { if (Session["Invalid"] == null) { bool insertresult = false; DateTime d1 = (DateTime)Session["StartDate"]; DateTime d2 = (DateTime)Session["EndDate"]; Systemtime st = new Systemtime(d1, d2); bool checkexistmonth = dbmanager.CheckExistMonth(d2); if (checkexistmonth == false) { bool result = dbmanager.DeleteSystemTime(); if (result == true) { insertresult = dbmanager.InsertSystemTime(st); } } if (insertresult == true) { dbmanager.DeletePreviousAllSaved(); Messaglbl.Text = "Dates updated successfully."; Systemtime sttime = dbmanager.GetSystemTime(); if (sttime != null) { startlbl.Text = sttime.Startdate.ToLongDateString(); endlbl.Text = sttime.Enddate.ToLongDateString(); } else { startlbl.Text = "--"; endlbl.Text = "--"; } } else { Messaglbl.Text = "Fail to update date. Duplicate month might exist for the appraisal."; } } } } else { Messaglbl.Text = "The dates are invalid, please reselect the dates."; } } else { MessageBoxShow("The dates are invalid, please reselect the dates."); } }