public AddressDTO FindBy(int id) { AddressDTO address; CityDTO city; string queryString = "SELECT * FROM dbo.Address WHERE addressID = @id"; try { //The connection is automatically closed at the end of the using block. using (SqlConnection con = new SqlConnection(ConnectionString)) { using (SqlCommand cmd = new SqlCommand(queryString, con)) { cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = id; con.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { address = new AddressDTO(); city = new CityDTO(); address = address = GenerateAddress(reader, address, city); //return product instance as data object Debug.Print("AddressDAL: /FindByID/ " + address.GetID()); return(address); } } } } catch (Exception e) { e.GetBaseException(); } return(null); }
private static AccountDTO GenerateAccount(SqlDataReader reader, AccountDTO account, AddressDTO address) { if (reader["addressID"] != DBNull.Value) { address.SetID(Convert.ToInt32(reader["addressID"])); Debug.Print("AccountDAL / GenerateAccount: " + address.GetID()); account.SetAddress(address); } account.SetID(Convert.ToInt32(reader["accountID"])); account.SetEmail(reader["email"].ToString()); account.SetPw((byte[])reader["md5pw"]); account.SetSalt((byte[])reader["salt"]); account.SetFirstName(reader["firstName"].ToString()); account.SetLastName(reader["lastName"].ToString()); account.SetBirthdate(Convert.ToDateTime(reader["birthDate"])); account.SetPhoneNo(reader["phone"].ToString()); account.SetImgPath(reader["imgPath"].ToString()); account.SetIsAdmin(Convert.ToByte(reader["isAdmin"])); account.SetIsConfirmed(Convert.ToByte(reader["isConfirmed"])); account.SetConfirmationID(Convert.ToInt32(reader["confirmationID"])); account.SetStatus(Convert.ToByte(reader["status"])); return(account); }