private static void LoadVendorFromSql(Vendors vendor, SqlDataReader reader) { vendor.ID = (int)reader["Id"]; vendor.Code = reader["Code"].ToString(); vendor.Name = reader["Name"].ToString(); vendor.Address = reader["Address"].ToString(); vendor.City = reader["City"].ToString(); vendor.State = reader["State"].ToString(); vendor.Zip = reader["Zip"].ToString(); vendor.Phone = reader["Phone"]?.ToString(); vendor.Email = reader["Email"]?.ToString(); }
private static void SetParameterValues(Vendors vendor, SqlCommand sqlcmd) { sqlcmd.Parameters.AddWithValue("@Id", vendor.ID); sqlcmd.Parameters.AddWithValue("@Code", vendor.Code); sqlcmd.Parameters.AddWithValue("@Name", vendor.Name); sqlcmd.Parameters.AddWithValue("@Address", vendor.Address); sqlcmd.Parameters.AddWithValue("@City", vendor.City); sqlcmd.Parameters.AddWithValue("@State", vendor.State); sqlcmd.Parameters.AddWithValue("@Zip", vendor.Zip); sqlcmd.Parameters.AddWithValue("@Phone", vendor.Phone); sqlcmd.Parameters.AddWithValue("@Email", vendor.Email); }
// try collapsing the block below. The region stuff stays. To create region: Type #, look at dropdown menu #region Instance Properties private static void LoadVendorFromSql(Vendors vendor, SqlDataReader myReader) { vendor.Id = ((int)myReader["Id"]); vendor.Code = myReader["Code"].ToString(); //this is the best conversion, will continute with it now vendor.Name = myReader["Name"].ToString(); vendor.Address = myReader["Address"].ToString(); vendor.City = myReader["City"].ToString(); vendor.State = myReader["State"].ToString(); vendor.Zip = myReader["Zip"].ToString(); vendor.Phone = myReader["Phone"]?.ToString(); // note the ? vendor.Email = myReader["Email"]?.ToString(); // note the ? #endregion }
public static List <Vendors> GetAll() { var sqlcmd = new SqlCommand(SqlGetAll, Connection.sqlConnection); var reader = sqlcmd.ExecuteReader(); var vendors = new List <Vendors>(); while (reader.Read()) { var vendor = new Vendors(); vendors.Add(vendor); LoadVendorFromSql(vendor, reader); } reader.Close(); return(vendors); }
// GET ALL public static List <Vendors> GetAll() { // don't need sql statement this time, because we defined it up above var sqlcmd = new SqlCommand(SqlGetAll, Connection.sqlConnection); // the underrscore is actually a SqlConnection. So he would like to rename this: _Connection changed to sqlConnection var myReader = sqlcmd.ExecuteReader(); var vendors = new List <Vendors>(); while (myReader.Read()) { var vendor = new Vendors(); vendors.Add(vendor); //"I do that, even though I haven't filled it with data, so I don't forget to do it" LoadVendorFromSql(vendor, myReader); } myReader.Close(); return(vendors); }
public static Vendors GetByPk(int id) { var sqlcmd = new SqlCommand(SqlGetByPk, Connection.sqlConnection); sqlcmd.Parameters.AddWithValue("@Id", id); var reader = sqlcmd.ExecuteReader(); if (!reader.HasRows) { reader.Close(); return(null); } reader.Read(); var vendor = new Vendors(); LoadVendorFromSql(vendor, reader); reader.Close(); return(vendor); }
public static Vendors GetByCode(string code) { var sqlcmd = new SqlCommand(SQLGetByCode, Connection.sqlConnection); sqlcmd.Parameters.AddWithValue("@Code", code); var reader = sqlcmd.ExecuteReader(); if (!reader.HasRows) { reader.Close(); return(null); } reader.Read(); var vendor = new Vendors(); LoadVendorFromSql(vendor, reader); reader.Close(); return(vendor); }
public static List <Products> GetAll() { var sqlcmd = new SqlCommand(SqlGetAll, Connection.sqlConnection); var reader = sqlcmd.ExecuteReader(); var products = new List <Products>(); while (reader.Read()) { var product = new Products(); products.Add(product); LoadProductFromSql(product, reader); } reader.Close(); Vendors.Connection = Connection; foreach (var prod in products) { var vendor = Vendors.GetByPk(prod.VendorID); // adding a FK prod.Vendor = vendor; } return(products); }