protected void btnAccept_Click(object sender, EventArgs e) { if (validateControl()) { Competition c = new Competition(); c.Remark = txtRemark.Text; if (cbStaff.SelectedValue != null) c.StaffId = Convert.ToInt32(cbStaff.SelectedValue); c.StartDate = DateTime.Parse(txtStartDate.Text); c.DueDate = DateTime.Parse(txtEndDate.Text); c.Topic = txtTopic.Text; c.Condition = txtConditition.Text; c.CompetitionDescription = txtDescription.Text; if (CompetitionDAO.Create(c)) { Flash.dictFlash.Add("success", String.Format("Created competition [<b>{0}</b>] successfully", c.Topic)); Response.Redirect("Index.aspx"); } else { Flash.dictFlash.Add("danger", " Cannot create competition !!!"); Response.Redirect("New.aspx"); } } }
public static bool Create(Competition c) { DBUtilities.Connection(); try { string sql = "INSERT INTO [Competitions] (StaffId,Topic,StartDate,DueDate,CompetitionDescription,Condition,Remark)"; sql += "VALUES (@1,@2,@3,@4,@5,@6,@7)"; SqlCommand cmd = new SqlCommand(sql, DBUtilities.objConnection); cmd.Parameters.AddWithValue("@1", c.StaffId); cmd.Parameters.AddWithValue("@2", c.Topic); cmd.Parameters.AddWithValue("@3", c.StartDate); cmd.Parameters.AddWithValue("@4", c.DueDate); cmd.Parameters.AddWithValue("@5", c.CompetitionDescription); cmd.Parameters.AddWithValue("@6", c.Condition); if(c.Remark != null) cmd.Parameters.AddWithValue("@7", c.Remark); else cmd.Parameters.AddWithValue("@7", ""); cmd.ExecuteNonQuery(); cmd.Dispose(); return true; } catch (Exception) { return false; } finally { DBUtilities.Close_Connection(); } }
public static bool Destroy(Competition c) { DBUtilities.Connection(); try { string sql = "DELETE FROM [Competitions] WHERE Id = @1"; SqlCommand comand = DBUtilities.objConnection.CreateCommand(); comand.CommandText = sql; comand.Parameters.AddWithValue("@1", c.Id); comand.ExecuteNonQuery(); comand.Dispose(); return true; } catch (Exception) { return false; } finally { DBUtilities.Close_Connection(); } }
protected void btnAdd_Click(object sender, EventArgs e) { if (Validate_Add()) { User u = (User)Session["current_user"]; Competition c = new Competition(); c.StaffId = u.Id; c.StartDate = DateTime.Parse(txtStartDate.Text); c.DueDate = DateTime.Parse(txtDueDate.Text); c.Topic = txtTopic.Text; c.Condition = txtCondition.Text; c.CompetitionDescription = txtDesc.Text; if (CompetitionDAO.Create(c)) { Flash.dictFlash.Add("success", String.Format("Created competition [<b>{0}</b>] successfully", c.Topic)); Response.Redirect("List.aspx"); } else { Flash.dictFlash.Add("danger", " Cannot create competition !!!"); Response.Redirect("List.aspx"); } } }
public static Competition Find(int id) { DBUtilities.objConnection = new SqlConnection(DBUtilities.connStr); DataTable dt = new DataTable(); string sql = "Select * from [Competitions] WHERE Id = @1"; SqlDataAdapter adap = new SqlDataAdapter(sql, DBUtilities.objConnection); adap.SelectCommand.Parameters.AddWithValue("@1", id); adap.Fill(dt); if (dt.Rows.Count > 0) { Competition c = new Competition(); c.Id = Convert.ToInt32(dt.Rows[0]["Id"]); c.StaffId = Convert.ToInt32(dt.Rows[0]["StaffId"]); c.Topic = dt.Rows[0]["Topic"].ToString(); c.StartDate = Convert.ToDateTime(dt.Rows[0]["StartDate"]); c.DueDate = Convert.ToDateTime(dt.Rows[0]["DueDate"]); c.CompetitionDescription = dt.Rows[0]["CompetitionDescription"].ToString(); c.Condition = dt.Rows[0]["Condition"].ToString(); c.Remark = dt.Rows[0]["Remark"].ToString(); return c; } return null; }
public static bool Update(Competition c) { DBUtilities.Connection(); try { string sql = "Update [Competitions] set "; Type myType = c.GetType(); IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties()); int i = 1; int j = 1; foreach (PropertyInfo prop in props) { object propValue = prop.GetValue(c, null); if (propValue != null && prop.Name != "Id") { if (j < props.Count) sql += String.Format("{0} = @{1} ,", prop.Name, i); else sql += String.Format("{0} = @{1} where ID= @{2}", prop.Name, i, i + 1); i++; } j++; } i = 1; j = 1; SqlCommand cmd = new SqlCommand(sql, DBUtilities.objConnection); foreach (PropertyInfo prop in props) { object propValue = prop.GetValue(c, null); if (propValue != null && prop.Name != "Id") { if (j < props.Count) cmd.Parameters.AddWithValue(String.Format("@{0}", i), propValue); else { cmd.Parameters.AddWithValue(String.Format("@{0}", i), propValue); cmd.Parameters.AddWithValue(String.Format("@{0}", i + 1), c.Id); } i++; } j++; } cmd.ExecuteNonQuery(); cmd.Dispose(); return true; } catch (Exception) { return false; } finally { DBUtilities.Close_Connection(); } }