public static bool Create(Customer u) { DBUtilities.Connection(); try { string sql = "Insert into Customers (CustomerEmail,CustomerName,CustomerAddress,CustomerGender,CustomerBirthday,CustomerPhone)"; sql += " values (@1,@2,@3,@4,@5,@6)"; SqlCommand cmd = new SqlCommand(sql, DBUtilities.objConnection); cmd.Parameters.AddWithValue("@1", u.CustomerEmail); cmd.Parameters.AddWithValue("@2", u.CustomerName); cmd.Parameters.AddWithValue("@3", u.CustomerAddress); cmd.Parameters.AddWithValue("@4", u.CustomerGender); cmd.Parameters.AddWithValue("@5", u.CustomerBirthday); cmd.Parameters.AddWithValue("@6", u.CustomerPhone); cmd.ExecuteNonQuery(); cmd.Dispose(); return true; } catch (Exception) { return false; } finally { DBUtilities.Close_Connection(); } }
protected void btnAccept_Click(object sender, EventArgs e) { int Id = (Request.QueryString["ID"] != null) ? Convert.ToInt32(Request.QueryString["ID"]) : 1; string format = "yyyy-MM-dd HH:mm:ss"; if (Validate_Control()) { Customer c = new Customer(); c.CustomerName = txtFullname.Text; c.CustomerEmail = txtEmail.Text; if (rdbMale.Checked) c.CustomerGender = false; else c.CustomerGender = true; c.CustomerBirthday = Convert.ToDateTime(txtBirthday.Text); ; c.CustomerAddress = txtAddress.Text; c.CustomerPhone = txtPhone.Text; if (CustomerDAO.Create(c)) { DataTable dtCus = CustomerDAO.All(); Painting paint = PaintingDAO.Find(Convert.ToInt32(PaintingId.Value)); paint.IsPaid = true; paint.LastModify = DateTime.Now; paint.CustomerId = Convert.ToInt32(dtCus.Rows[0][0]); if (PaintingDAO.Update(paint)) { Flash.dictFlash.Add("success", String.Format("Order successful, Wait for call")); Response.Redirect("Show.aspx?ID=" + Id); } else { Flash.dictFlash.Add("danger", "Have error in order process, Pless call"); Response.Redirect("Show.aspx?ID=" + Id); } } else { Flash.dictFlash.Add("danger", "You can't order with this picture"); Response.Redirect("Show.aspx?ID=" + Id); } } }
public static bool Destroy(Customer c) { DBUtilities.Connection(); try { string sql = "Delete from Customers where Id = @1"; SqlCommand cmd = new SqlCommand(sql, DBUtilities.objConnection); cmd.Parameters.AddWithValue("@1", c.Id); cmd.ExecuteNonQuery(); cmd.Dispose(); return true; } catch (Exception) { return false; } finally { DBUtilities.Close_Connection(); } }
public static Customer Find(int Id) { DBUtilities.objConnection = new SqlConnection(DBUtilities.connStr); DataTable dt = new DataTable(); string sql = "Select * from Customers where Id = @Id"; SqlDataAdapter adap = new SqlDataAdapter(sql, DBUtilities.objConnection); adap.SelectCommand.Parameters.AddWithValue("@Id", Id); adap.Fill(dt); if (dt.Rows.Count > 0) { Customer c = new Customer(); c.Id = Convert.ToInt32(dt.Rows[0]["Id"]); c.CustomerName = dt.Rows[0]["CustomerName"].ToString(); c.CustomerEmail = dt.Rows[0]["CustomerEmail"].ToString(); c.CustomerGender = bool.Parse(dt.Rows[0]["CustomerGender"].ToString()); c.CustomerBirthday = DateTime.Parse(dt.Rows[0]["CustomerBirthday"].ToString()); c.CustomerAddress = dt.Rows[0]["CustomerAddress"].ToString(); c.CustomerPhone = dt.Rows[0]["CustomerPhone"].ToString(); return c; } return null; }
public static bool Update(Customer u) { DBUtilities.Connection(); try { string sql = "Update Customers set "; Type myType = u.GetType(); IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties()); int i = 1; int j = 1; foreach (PropertyInfo prop in props) { object propValue = prop.GetValue(u, 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(u, 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), u.Id); } i++; } j++; } cmd.ExecuteNonQuery(); cmd.Dispose(); return true; } catch (Exception) { return false; } finally { DBUtilities.Close_Connection(); } }