private void btnSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(txtCatDesc.Text)) { MessageBox.Show("Description is required", "Validation"); return; } Category cat = new Category(); cat.CategoryId = string.IsNullOrEmpty(txtCatId.Text) ? 0 : Convert.ToInt16(txtCatId.Text); cat.Description = txtCatDesc.Text; cat.AddedDate = DateTime.Now; cat.Inactive = chkInactive.Checked; //Calls the manager. result = library.SaveCategory(cat); if (result) { MessageBox.Show("Category Saved Successfully", "Information"); } } catch (Exception ex) { throw ex; } }
public bool Save(Category category) { const string sqlStatement = "INSERT INTO dbo.category (Description, AddedDate, Inactive) " + "VALUES(@description, @addeddate, @inactive); "; //Connect to database. con = OpenConnection(); com = new SqlCommand(sqlStatement, con); //Set parameter values. com.Parameters.AddWithValue("@description", category.Description); com.Parameters.AddWithValue("@addeddate", category.AddedDate); com.Parameters.AddWithValue("@inactive", category.Inactive); using (TransactionScope scope = new TransactionScope()) { try { com.ExecuteNonQuery(); scope.Complete(); return true; } catch (Exception) { scope.Dispose(); throw; } finally { scope.Dispose(); } } }
public bool SaveCategory(Category cat) { bool result = false; CategoryDAO cd = new CategoryDAO(); //Save or Update. result = cat.CategoryId == 0 ? cd.Save(cat) : cd.Update(cat); return result; }
public Category Select(int categoryId) { const string sqlStatement = "SELECT CategoryId, Description, AddedDate, Inactive " + "FROM dbo.Category " + "WHERE CategoryId=@categoryId "; //Connect to database. con = OpenConnection(); //Create a new category. Category category = new Category(); com = new SqlCommand(sqlStatement, con); com.Parameters.AddWithValue("@CategoryId", categoryId); try { using (IDataReader dr = com.ExecuteReader()) { if (dr.Read()) { category = new Category { CategoryId = GetDataValue<int>(dr, "CategoryId"), Description = GetDataValue<string>(dr, "Description"), AddedDate = GetDataValue<DateTime>(dr, "AddedDate"), Inactive = GetDataValue<bool>(dr, "Inactive") }; } } return category; } catch (Exception ex) { throw ex; } }
public bool Update(Category category) { const string sqlStatement = "UPDATE dbo.category SET Description=@description, Inactive=@inactive " + "WHERE CategoryId=@categoryid"; //Connect to database. con = OpenConnection(); com = new SqlCommand(sqlStatement, con); //Set parameter values. com.Parameters.AddWithValue("@categoryid", category.CategoryId); com.Parameters.AddWithValue("@description", category.Description); com.Parameters.AddWithValue("@inactive", category.Inactive); using (TransactionScope scope = new TransactionScope()) { try { com.ExecuteNonQuery(); scope.Complete(); return true; } catch (Exception) { scope.Dispose(); throw; } finally { scope.Dispose(); } } }