//takes the required values and add bus related information to database public static string AddBus(string name, string type, int capacity) { try { var b = from L in d.BusDetails_Table where L.Bus_Name.ToUpper() == name.ToUpper() && L.Type.ToUpper() == type.ToUpper() select L; if (b.Count() > 0) { return("Bus already exists"); } int rid = (from r in d.BusDetails_Table select r).Count() + 1; BusDetails_Table Bt = new BusDetails_Table(); Bt.Bus_ID = "B" + string.Format("{0:000}", rid); Bt.Bus_Name = name; Bt.Type = type; Bt.Capacity = capacity; d.BusDetails_Table.Add(Bt); d.SaveChanges(); return("Bus details created for " + Bt.Bus_Name + " of type " + Bt.Type + " with capacity " + Bt.Capacity + ". Bus ID is " + Bt.Bus_ID); } catch (DbUpdateException E) { SqlException ex = E.GetBaseException() as SqlException; if (ex.Message.Contains("PK_BusDetails_Table")) { return("Bus id already exists"); } } return(null); }
//takes the required values and add schedule related information to database public static string AddSchedule(addSchedule a) { try { var b = from L in d.BusDetails_Table where L.Bus_ID == a.Bid select L; var s = from K in d.Schedule_Table select K; List <Schedule_Table> sc = s.ToList(); int flag = 0; foreach (var c in sc) { if (a.Doj == c.DateOfJourney && a.Bid == c.Bus_ID) { flag = 1; break; } } BusDetails_Table B = b.FirstOrDefault(); if (B.Capacity >= a.Capacity) { if (flag == 0) { int sid = (from r in d.Schedule_Table select r).Count() + 1; Schedule_Table Sh = new Schedule_Table(); Sh.SID = "S" + sid; Sh.Route_ID = a.Rid; Sh.Bus_ID = a.Bid; Sh.StartTime = a.StartTime.TimeOfDay; Sh.DateOfJourney = a.Doj; Sh.AvailableSeats = a.Capacity; d.Schedule_Table.Add(Sh); d.SaveChanges(); return("Schedule created with Schedule id " + Sh.SID); } else { return("Journey already exist"); } } else { return("Available seats are more than capacity"); } } catch (DbUpdateException E) { SqlException ex = E.GetBaseException() as SqlException; } return(null); }