public void SavePublisher(string pubID, string name, string city) { Validate._Error e = new Validate._Error(); e.Msg = string.Empty; e.Caught = false; if (!Validate.IsFieldPopulated(name.Trim())) { e = Validate.UpdateErrorObject(e, "Publisher name cannot be blank.", true); } else { if(!Validate.IsStringShortEnough(name, 40)) { e=Validate.UpdateErrorObject(e, "Publisher name cannot be longer than 40 characters.", true); } } if (!Validate.IsStringShortEnough(city, 20)) { e = Validate.UpdateErrorObject(e, "City cannot be more than 20 characters.", true); } //If errors caught then throw exception with error message if (e.Caught) { throw new Exception(e.Msg); } //if we made it this far then no errors found using (pubsEntities context = new pubsEntities()) { publisher pub = context.publishers.FirstOrDefault(x => x.pub_id == pubID); pub.pub_name = name.Trim(); pub.city = city.Trim(); context.publishers.ApplyCurrentValues(pub); context.SaveChanges(); } }
public static void AddPub(string pubName, string city, string state) { StringBuilder error = new StringBuilder(); bool eCaught = false; if (pubName.Trim().Length > 40 || pubName.Trim().Length==0) { eCaught = true; error.Append("Publisher Name cannot be blank and cannot be longer than 40 characters. "); } if (city.Trim().Length > 20) { eCaught = true; error.Append("City cannot be longer than 20 characters."); } if (eCaught) throw new Exception(error.ToString()); using (pubsEntities context = new pubsEntities()) { publisher pub = new publisher(); //pub_id code must be a 4 digit code beginning with 99. get list of existing and find next available code int nextCode = 9900; string strNextCode = nextCode.ToString(); while (context.publishers.Any(x => x.pub_id == strNextCode)) { nextCode++; strNextCode = nextCode.ToString(); } pub.pub_id = strNextCode; pub.pub_name = pubName.Trim(); pub.city = city.Trim(); pub.state = state; try { context.publishers.AddObject(pub); context.SaveChanges(); } catch { throw new Exception("Sorry, the maximum number of publishers has been exceeded. Your publisher cannot be added."); } } }