public Customer GetCustomer(Customer newCustomer) { Customer customer = new Customer(); string cs = ConfigurationManager.ConnectionStrings["CRCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spGetCustomer", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameterId = new SqlParameter(); parameterId.ParameterName = "@Id"; parameterId.Value = newCustomer.Id; cmd.Parameters.Add(parameterId); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); bool isExist = false; while (reader.Read()) { isExist = true; if ((CustomerType)reader["CustomerType"] == CustomerType.PayAsGoCustomer) { customer = new PayAsGoCustomer { CustomerType = CustomerType.PayAsGoCustomer }; } else { customer = new ContractCustomer { CustomerType = CustomerType.ContractCustomer, Discount = Convert.ToDecimal(reader["Discount"]) }; } customer.Id = Convert.ToInt32(reader["Id"]); customer.FirstName = reader["FirstName"].ToString(); customer.LastName = reader["LastName"].ToString(); customer.Mobile = reader["Mobile"].ToString(); customer.Email = reader["Email"].ToString(); } if (!isExist) { throw new FaultException("Customer id " + newCustomer.Id.ToString() + " does not exist"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } return(customer); }
public List <Customer> GetCustomers() { List <Customer> customers = new List <Customer>(); Customer customer = new Customer(); string cs = ConfigurationManager.ConnectionStrings["CRCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spGetCustomers", con); cmd.CommandType = CommandType.StoredProcedure; con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { if ((CustomerType)reader["CustomerType"] == CustomerType.PayAsGoCustomer) { customer = new PayAsGoCustomer { CustomerType = CustomerType.PayAsGoCustomer }; } else { customer = new ContractCustomer { CustomerType = CustomerType.ContractCustomer, Discount = Convert.ToDecimal(reader["Discount"]) }; } customer.Id = Convert.ToInt32(reader["Id"]); customer.FirstName = reader["FirstName"].ToString(); customer.LastName = reader["LastName"].ToString(); customer.Mobile = reader["Mobile"].ToString(); customer.Email = reader["Email"].ToString(); customers.Add(customer); } } return(customers); }