public virtual void Delete() { if (this.IsNewRecord) { return; } KeyValue pkValue = KeyValue.XmlToKey(this.RecordUniqueId); LocationsTable.DeleteRecord(pkValue); }
public virtual LocationsRecord GetRecord() { if (this.DataSource != null) { return(this.DataSource); } if (this.RecordUniqueId != null) { return(LocationsTable.GetRecord(this.RecordUniqueId, true)); } // Localization. throw new Exception(Page.GetResourceValue("Err:RetrieveRec", "RatTrap")); }
private void ListLocations(CommandContext context) { IEnumerable <Location> locations = context.Factory.Locations.List(null, 1, int.MaxValue); if (locations.Any()) { context.Output.WriteLine($"There are {locations.Count()} locations in the database:\n"); LocationsTable table = new LocationsTable(locations); table.PrintTable(context.Output); } else { context.Output.WriteLine("There are no locations in the database"); } context.Output.Flush(); }
public virtual void LoadData() { // Load the data from the database into the DataSource DatabaseTheRatTrap%dbo.Locations record. // It is better to make changes to functions called by LoadData such as // CreateWhereClause, rather than making changes here. // The RecordUniqueId is set the first time a record is loaded, and is // used during a PostBack to load the record. if (this.RecordUniqueId != null && this.RecordUniqueId.Length > 0) { this.DataSource = LocationsTable.GetRecord(this.RecordUniqueId, true); return; } // This is the first time a record is being retrieved from the database. // So create a Where Clause based on the staic Where clause specified // on the Query wizard and the dynamic part specified by the end user // on the search and filter controls (if any). WhereClause wc = this.CreateWhereClause(); // If there is no Where clause, then simply create a new, blank record. if (wc == null || !(wc.RunQuery)) { this.DataSource = new LocationsRecord(); return; } // Retrieve the record from the database. It is possible LocationsRecord[] recList = LocationsTable.GetRecords(wc, null, 0, 2); if (recList.Length == 0) { // There is no data for this Where clause. wc.RunQuery = false; return; } // Set DataSource based on record retrieved from the database. this.DataSource = (LocationsRecord)LocationsRecord.Copy(recList[0], false); }
public Tuple <string, string> CalculateDistanceDuration(string origin, string destination) { try { #region Using MSSQL Database Distance Finder string org = origin; string des = destination; //region for the origin search #region Origin Search LocationsTable objLocationOrigin = new LocationsTable(); if (org.Contains(" ")) { for (int i = 0; i < origin.Split().Length; i++) { //check whether the entered loacation available in the database if (context.LocationsTables.Any(o => o.City == org)) { objLocationOrigin = context.LocationsTables.Where(x => x.City == org).FirstOrDefault(); break; } else { org = org.Substring(0, org.LastIndexOf(" ") + 1); org = org.Trim(); if (org.EndsWith(",")) { org = org.Substring(0, org.Length - 1); } } } } else { if (context.LocationsTables.Any(o => o.City == org)) { objLocationOrigin = context.LocationsTables.Where(x => x.City == org).FirstOrDefault(); } } #endregion //region for the destination search #region Destination search LocationsTable objLocationDestination = new LocationsTable(); if (des.Contains(" ")) { for (int i = 0; i < destination.Split().Length; i++) { //check whether the entered loacation available in the database if (context.LocationsTables.Any(o => o.City == des)) { objLocationDestination = context.LocationsTables.Where(x => x.City == des).FirstOrDefault(); break; } else { des = des.Substring(0, des.LastIndexOf(" ") + 1); des = des.Trim(); if (des.EndsWith(",")) { des = des.Substring(0, des.Length - 1); } } } } else { if (context.LocationsTables.Any(o => o.City == des)) { objLocationDestination = context.LocationsTables.Where(x => x.City == des).FirstOrDefault(); } } #endregion //Calculate the distance if (objLocationOrigin.LocationID != 0 && objLocationDestination.LocationID != 0) { var oCoord = new GeoCoordinate(Convert.ToDouble(objLocationOrigin.Latitude), Convert.ToDouble(objLocationOrigin.Longtitude)); var dCoord = new GeoCoordinate(Convert.ToDouble(objLocationDestination.Latitude), Convert.ToDouble(objLocationDestination.Longtitude)); return(new Tuple <string, string>(oCoord.GetDistanceTo(dCoord).ToString(), "4")); } #endregion else if (origin != null && destination != null) { #region Using Google Maps API string url = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + origin + "&destinations=" + destination + "&mode=driving&sensor=false&language=en-EN&units=metric&key=AIzaSyDdsaERISYyGWXAR5s8aBaumYo4gGrOxh0"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader sreader = new StreamReader(dataStream); string responsereader = sreader.ReadToEnd(); response.Close(); XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(responsereader); string distanceMeter = "0"; string durationMin = "0"; if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK") { XmlNodeList distance = xmldoc.GetElementsByTagName("distance"); distanceMeter = distance[0].ChildNodes[0].InnerText.ToString(); XmlNodeList duration = xmldoc.GetElementsByTagName("duration"); durationMin = duration[0].ChildNodes[0].InnerText.ToString(); Tuple <string, string> t = new Tuple <string, string>(distanceMeter, durationMin); return(t); } else { return(new Tuple <string, string>("1000", "4")); } #endregion } else { Tuple <string, string> t = new Tuple <string, string>("1000", "4"); return(t); } } catch (Exception e) { Tuple <string, string> t = new Tuple <string, string>("1000", "4"); return(t); } }