public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT CrimeDate, COUNT(CrimeDate) AS NumCrimes, ROUND ((CAST(COUNT(CrimeDate) AS float) / ((SELECT AVG(CrimeAvg) FROM (SELECT CrimeDate, COUNT(CID) as CrimeAvg FROM Crimes WHERE Year = 2015 GROUP BY CrimeDate) MyTable)) * 100) - 100 , 2) AS AVGDifference FROM Crimes WHERE CrimeDate IN ('01/01/2015','01/19/2015','05/25/15','07/04/2015','09/07/2015','11/26/2015','12/25/2015') GROUP BY CrimeDate"); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.CrimeDate = Convert.ToString(row["CrimeDate"]); m.NumCrimes = Convert.ToInt32(row["NumCrimes"]); m.CrimePercent = Convert.ToDouble(row["AVGDifference"]); crimes.Add(m); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT TOP 10 Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, Count(Crimes.IUCR) AS NumOccured, (SUM(CONVERT(float,Arrested))/COUNT(*)) AS ArrestPercent FROM Crimes INNER JOIN Codes ON Crimes.IUCR = Codes.IUCR GROUP BY Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc ORDER BY NumOccured DESC; "); string sql2 = string.Format(@" SELECT COUNT(*) AS TotalCrimes from Crimes "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); DataSet ds2 = DataAccessTier.DB.ExecuteNonScalarQuery(sql2); foreach (DataRow row in ds.Tables["TABLE"].Rows) { foreach(DataRow row2 in ds2.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); double NumOccured = Convert.ToDouble(row["NumOccured"]); double TotalCrimes = Convert.ToDouble(row2["TotalCrimes"]); double ArrestPercent = Convert.ToDouble(row["ArrestPercent"]); c.IUCR = Convert.ToInt32(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.NumOccured = Convert.ToInt32(row["NumOccured"]); c.PercentTotal = Convert.ToDouble((NumOccured/TotalCrimes)*100).ToString("0.00"); c.ArrestPercent = Convert.ToDouble((ArrestPercent)*100).ToString("0.00"); crimes.Add(c); } } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT Areas.Area, Areas.AreaName, Count(Crimes.Area) AS NumOccured FROM Crimes LEFT JOIN Areas ON Crimes.Area = Areas.Area GROUP BY Areas.AreaName, Areas.Area ORDER BY Areas.AreaName ASC; "); string sql2 = string.Format(@" SELECT COUNT(*) AS TotalCrimes from Crimes "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); DataSet ds2 = DataAccessTier.DB.ExecuteNonScalarQuery(sql2); double TotalCrimes = Convert.ToDouble(ds2.Tables["TABLE"].Rows[0]["TotalCrimes"].ToString()); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); double NumOccured = Convert.ToDouble(row["NumOccured"]); c.Area = Convert.ToInt32(row["Area"]); c.AreaName = Convert.ToString(row["AreaName"]); c.NumOccured = Convert.ToInt32(row["NumOccured"]); c.PercentTotal = Convert.ToDouble((NumOccured/TotalCrimes)*100).ToString("0.00"); crimes.Add(c); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List <Models.Crime> topTen = new List <Models.Crime>(); // clear exception: EX = null; // try query try { string sql = string.Format(@" DECLARE @totalNumCrimes INT = (SELECT Count(*) FROM Crimes) SELECT TOP 10 Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, Count(*) AS TotalNumTimes, CONVERT(decimal(3,2), ( CONVERT( float, COUNT(Crimes.IUCR))/@totalNumCrimes ) * 100 ) AS CrimePercentage, CONVERT(decimal(5,2), ( CONVERT( float, SUM( CONVERT(float, Crimes.Arrested) ) ) / CONVERT( float, Count(Crimes.IUCR)) ) * 100 ) AS ArrestPercentage FROM Crimes INNER JOIN Codes ON Codes.IUCR = Crimes.IUCR INNER JOIN Areas ON Areas.Area = Crimes.Area GROUP BY Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc ORDER BY TotalNumTimes DESC; "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.NumTimes = Convert.ToInt32(row["TotalNumTimes"]); c.CrimePercentage = Convert.ToSingle(row["CrimePercentage"]); c.ArrestPercentage = Convert.ToSingle(row["ArrestPercentage"]); topTen.Add(c); } } // end try catch (Exception ex) { EX = ex; } finally { TopTenList = topTen; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT top 10 Codes.IUCR,Codes.PrimaryDesc,Codes.SecondaryDesc, COUNT(*) AS TotalNumOfCrimes, ROUND((CONVERT(float,COUNT(*))*100.0/(SELECT COUNT(*) FROM Crimes)),2) AS percentOfCrime,ROUND(AVG(CONVERT(float,Arrested))*100.0,2) AS PercentageOfArrest FROM Crimes INNER JOIN Codes ON Crimes.IUCR = Codes.IUCR GROUP BY Codes.IUCR,Codes.PrimaryDesc,Codes.SecondaryDesc ORDER BY TotalNumOfCrimes DESC; "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"])+" "+ Convert.ToString(row["SecondaryDesc"]);; //c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.TotalNumOfCrimes = Convert.ToInt32(row["TotalNumOfCrimes"]); c.PercentOfCrime = Convert.ToDouble(row["PercentOfCrime"]); c.PercentageOfArrest = Convert.ToDouble(row["PercentageOfArrest"]); crimes.Add(c); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT Areas.Area, Areas.AreaName, Count(*) AS NumCrimes, Crimes.Year FROM Crimes FULL OUTER JOIN Areas ON Crimes.Area = Areas.Area WHERE Areas.Area != 0 GROUP BY Crimes.Year, Areas.Area, Areas.AreaName ORDER BY Crimes.Year DESC, NumCrimes DESC; "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.Area = Convert.ToInt32(row["Area"]); c.AreaName = Convert.ToString(row["AreaName"]); c.NumOccured = Convert.ToInt32(row["NumCrimes"]); c.Year = Convert.ToInt32(row["Year"]); crimes.Add(c); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT TOP 10 Crimes.IUCR, PrimaryDesc, SecondaryDesc, Count(CID) AS NumOccur, ROUND(CAST(COUNT(CID) AS FLOAT)/(SELECT COUNT(CID) FROM Crimes)*100, 2) AS 'Crime %', ROUND(CAST(COUNT(CASE WHEN Arrested = 1 THEN 1 END)AS FLOAT)/COUNT(Crimes.IUCR)*100, 2) AS 'Arrested %' FROM Crimes INNER JOIN codes ON Crimes.IUCR = Codes.IUCR GROUP BY Crimes.IUCR, PrimaryDesc, SecondaryDesc ORDER BY numOccur DESC; "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.NumOccur = Convert.ToInt32(row["NumOccur"]); c.CrimePer = Convert.ToDouble(row["Crime %"]); c.ArrestedPer = Convert.ToDouble(row["Arrested %"]); crimes.Add(c); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT MONTH(CrimeDate) AS Month, COUNT(CrimeDate) AS NumOccur, ROUND(CAST(COUNT(CrimeDate) AS FLOAT)/(SELECT COUNT(CrimeDate) FROM Crimes INNER JOIN Codes ON Crimes.IUCR = Codes.IUCR WHERE PrimaryDesc = 'HOMICIDE')*100, 2) AS 'Homicide %' FROM Crimes INNER JOIN Codes ON Crimes.IUCR = Codes.IUCR WHERE PrimaryDesc = 'HOMICIDE' GROUP BY MONTH(CrimeDate) ORDER BY 'Homicide %' DESC "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.Month = Convert.ToInt32(row["Month"]); c.NumOccur = Convert.ToInt32(row["NumOccur"]); c.CrimePer = Convert.ToDouble(row["Homicide %"]); crimes.Add(c); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@"SELECT top 10 Crimes.IUCR,PrimaryDesc,SecondaryDesc, COUNT(Crimes.IUCR) as numCrimes, ROUND( (( CAST(COUNT(Crimes.IUCR) as float ) / (SELECT COUNT(CID) FROM Crimes))*100 ), 2) as CrimePercent, ROUND((( SUM(CAST(Arrested AS INT))) / (CAST(COUNT(Arrested) AS FLOAT) )) * 100 , 2) as ArrestedPercent FROM Codes LEFT JOIN Crimes ON (Codes.IUCR = Crimes.IUCR) GROUP BY Crimes.IUCR,PrimaryDesc,SecondaryDesc ORDER BY COUNT(Crimes.IUCR) DESC"); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.IUCR = Convert.ToString(row["IUCR"]); m.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); m.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); m.NumCrimes = Convert.ToInt32(row["numCrimes"]); m.CrimePercent = Convert.ToDouble(row["CrimePercent"]); m.ArrestedPercent = Convert.ToDouble(row["ArrestedPercent"]); crimes.Add(m); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List <Models.Crime> crimeList = new List <Models.Crime>(); // clear exception: EX = null; // try query try { string sql = string.Format(@" SELECT Codes.IUCR, PrimaryDesc, SecondaryDesc, COUNT(Codes.IUCR) AS Frequency FROM Codes FULL OUTER JOIN Crimes ON Crimes.IUCR = Codes.IUCR GROUP BY Codes.IUCR, PrimaryDesc, SecondaryDesc ORDER BY PrimaryDesc ASC, SecondaryDesc ASC; "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.NumTimes = Convert.ToInt32(row["Frequency"]); crimeList.Add(c); } } // end try catch (Exception ex) { EX = ex; } finally { CrimeList = crimeList; } } // end OnGet()
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@"SELECT table1.Year,October,November,December FROM (SELECT COUNT(Crimes.IUCR) as October, Year FROM Crimes LEFT JOIN Codes ON (Codes.IUCR = Crimes.IUCR) WHERE CrimeDate LIKE '%-10-%' and PrimaryDesc LIKE 'ASSAULT' GROUP BY Crimes.Year)table1 JOIN (SELECT COUNT(Crimes.IUCR) as November, Year FROM Crimes LEFT JOIN Codes ON (Codes.IUCR = Crimes.IUCR) WHERE CrimeDate LIKE '%-11-%' and PrimaryDesc LIKE 'ASSAULT' GROUP BY Crimes.Year)table2 ON (table1.Year = table2.Year) JOIN (SELECT COUNT(Crimes.IUCR) as December, Year FROM Crimes LEFT JOIN Codes ON (Codes.IUCR = Crimes.IUCR) WHERE CrimeDate LIKE '%-12-%' and PrimaryDesc LIKE 'ASSAULT' GROUP BY Crimes.Year)table3 ON (table1.Year = table3.Year) ORDER BY table1.Year ASC"); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.Year = Convert.ToString(row["Year"]); m.October = Convert.ToString(row["October"]); m.November = Convert.ToString(row["November"]); m.December = Convert.ToString(row["December"]); crimes.Add(m); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT Areas.Area, AreaName,COUNT(CID) as NumCrimes, ROUND(COUNT(CID) / CAST((SELECT COUNT(CID) FROM Crimes) AS float) * 100, 2) as PercentTotal FROM Areas LEFT JOIN Crimes ON (Areas.Area = Crimes.Area) WHERE NOT Areas.Area LIKE '0' GROUP BY Areas.Area, AreaName Order By AreaName"); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.Area = Convert.ToString(row["Area"]); m.AreaName = Convert.ToString(row["AreaName"]); m.NumCrimes = Convert.ToInt32(row["NumCrimes"]); m.CrimePercent = Convert.ToDouble(row["PercentTotal"]); crimes.Add(m); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet() { List<Models.Crime> crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT Codes.IUCR, PrimaryDesc,SecondaryDesc,COUNT(Crimes.IUCR) as NumCrimes FROM Codes LEFT JOIN Crimes on (Codes.IUCR = Crimes.IUCR) GROUP BY Codes.IUCR, PrimaryDesc,SecondaryDesc ORDER BY PrimaryDesc, SecondaryDesc"); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.IUCR = Convert.ToString(row["IUCR"]); m.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); m.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); m.NumCrimes = Convert.ToInt32(row["NumCrimes"]); crimes.Add(m); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet(string input) { List<Models.Crime> crimeYearList = new List<Models.Crime>(); // clear exception: EX = null; // initialize messages ErrorMsg = " "; NoResultMsg = " "; // make input available to web page: Input = input; try { // No input if (input == null) { // there's no page argument, perhaps user surfed to the page directly? // In this case, nothing to do. NoResultMsg = "Search results are empty."; } // User input exists else { // Lookup crime(s) based on input, which could be area number or area name: int id; string sql; // lookup crime by yea: if (System.Int32.TryParse(input, out id)) { sql = string.Format(@" SELECT Crimes.IUCR, Codes.PrimaryDesc, Crimes.Year, Areas.Area, Areas.AreaName FROM Crimes INNER JOIN Codes ON Codes.IUCR = Crimes.IUCR INNER JOIN Areas ON Areas.Area = Crimes.Area WHERE Crimes.Year = {0} GROUP BY Crimes.IUCR, Codes.PrimaryDesc, Crimes.Year, Areas.Area, Areas.AreaName ; ", id); } else { sql = " "; } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); if(ds.Tables[0].Rows.Count == 0) { NoResultMsg = "Search results are empty."; } else { foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.Year = Convert.ToInt32(row["Year"]); c.AreaName = Convert.ToString(row["AreaName"]); c.AreaNum = Convert.ToInt32(row["Area"]); crimeYearList.Add(c); } } } } // end try catch(Exception ex) { EX = ex; ErrorMsg = "Something went wrong. Could not find search results. Please try again."; } finally { CrimeYearList = crimeYearList; } } // end onGet()
public void OnGet() { List<Models.Crime> Crimes = new List<Models.Crime>(); // clear exception: EX = null; try { string sql = string.Format(@" SELECT TOP 10 Crimes.IUCR, PrimaryDesc, SecondaryDesc, Count(*) AS numTimes FROM Crimes INNER JOIN Codes ON Crimes.IUCR = Codes.IUCR GROUP BY Crimes.IUCR, PrimaryDesc, SecondaryDesc ORDER BY numTimes DESC; "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); //now for the total number of crimes string sql2 = string.Format(@" SELECT Count(*) AS total FROM Crimes; "); object result = DataAccessTier.DB.ExecuteScalarQuery(sql2); double totalCrimes = Convert.ToDouble(result); //now for the arrested string sql3 = string.Format(@" SELECT IUCR, Count(Arrested) as Arrested From Crimes Where Arrested=1 Group by IUCR "); DataSet ds2 = DataAccessTier.DB.ExecuteNonScalarQuery(sql3); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.IUCR = Convert.ToString(row["IUCR"]); m.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); m.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); m.numTimes = Convert.ToInt32(row["numTimes"]); m.percentChicago = m.numTimes/totalCrimes; Crimes.Add(m); } foreach (DataRow row in ds2.Tables["TABLE"].Rows) //this ds has all the crimes IUCRs and arrest numbers { string iucr = Convert.ToString(row["IUCR"]); double arrested = Convert.ToDouble(row["Arrested"]); foreach(Models.Crime c in Crimes) //search if this iucr is in our top 10 crimes list { if(c.IUCR == iucr){ //if the iucr is found in our top 10, assign its arrested number c.arrestRate = ((arrested/c.numTimes) *100); } } } } catch(Exception ex) { EX = ex; } finally { CrimeList = Crimes; } }
public void OnGet(string input) { List<Models.Crime> crimes = new List<Models.Crime>(); // make input available to web page: Input = input; // clear exception: EX = null; try { // // Do we have an input argument? If so, we do a lookup: // if (input == null) { // // there's no page argument, perhaps user surfed to the page directly? // In this case, nothing to do. // } else { // // Lookup movie(s) based on input, which could be id or a partial name: // int id; string sql; if (System.Int32.TryParse(input, out id)) { // lookup movie by area id: sql = string.Format(@" SELECT TOP 10 Areas.Area, Areas.AreaName, Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, Count(Crimes.IUCR) AS NumOccured, (SUM(CONVERT(float,Arrested))/COUNT(*)) AS ArrestPercent FROM Crimes LEFT JOIN Codes ON Crimes.IUCR = Codes.IUCR LEFT JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Area = {0} GROUP BY Areas.Area, Areas.AreaName, Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc ORDER BY NumOccured DESC; ", id); } else { // lookup movie(s) by partial name match: input = input.Replace("'", "''"); sql = string.Format(@" SELECT TOP 10 Areas.Area, Areas.AreaName, Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, Count(Crimes.IUCR) AS NumOccured, (SUM(CONVERT(float,Arrested))/COUNT(*)) AS ArrestPercent FROM Crimes LEFT JOIN Codes ON Crimes.IUCR = Codes.IUCR LEFT JOIN Areas ON Crimes.Area = Areas.Area WHERE AreaName LIKE '%{0}%' GROUP BY Areas.Area, Areas.AreaName, Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc ORDER BY NumOccured DESC; ", input); } string sql2 = string.Format(@" SELECT COUNT(*) AS TotalCrimes from Crimes "); DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); DataSet ds2 = DataAccessTier.DB.ExecuteNonScalarQuery(sql2); foreach (DataRow row in ds.Tables["TABLE"].Rows) { foreach (DataRow row2 in ds2.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); double NumOccured = Convert.ToDouble(row["NumOccured"]); double TotalCrimes = Convert.ToDouble(row2["TotalCrimes"]); double ArrestPercent = Convert.ToDouble(row["ArrestPercent"]); c.Area = Convert.ToInt32(row["Area"]); c.AreaName = Convert.ToString(row["AreaName"]); c.IUCR = Convert.ToInt32(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.NumOccured = Convert.ToInt32(row["NumOccured"]); c.PercentTotal = Convert.ToDouble((NumOccured/TotalCrimes)*100).ToString("0.00"); c.ArrestPercent = Convert.ToDouble((ArrestPercent)*100.0).ToString("0.00"); crimes.Add(c); } } }//else } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; NumAreas = crimes.Count; } }
public void OnGet(string input) { List <Models.Crime> crimes = new List <Models.Crime>(); // make input available to web page: Input = input; //clear exception: EX = null; try { if (input == null) { //nothing to do. } else { int id; string sql; if (System.Int32.TryParse(input, out id)) { //lookup area by area id sql = string.Format(@" SELECT MONTH(CrimeDate) AS Month, COUNT(CrimeDate) AS NumCrime, ROUND(CAST(COUNT(CrimeDate) AS FLOAT)/(SELECT COUNT(CrimeDate) FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Area = {0} )*100, 2) AS 'Crime %' FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Area = {0} GROUP BY MONTH(CrimeDate) ORDER BY Month ASC; ", id); } else { //lookup area by partial name match input = input.Replace("'", "''"); sql = string.Format(@" SELECT MONTH(CrimeDate) AS Month, COUNT(CrimeDate) AS NumCrime, ROUND(CAST(COUNT(CrimeDate) AS FLOAT)/(SELECT COUNT(CrimeDate) FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE AreaName LIKE '{0}' )*100, 2) AS 'Crime %' FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE AreaName LIKE '{0}' GROUP BY MONTH(CrimeDate) ORDER BY Month ASC ", input); } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); if (ds.Tables[0].Rows.Count == 0) { throw new Exception("Area not found"); } foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.Month = Convert.ToInt32(row["Month"]); c.NumCrime = Convert.ToInt32(row["NumCrime"]); c.CrimePer = Convert.ToDouble(row["Crime %"]); crimes.Add(c); } } } catch (Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet(String input) { List<Models.Crime> crimes = new List<Models.Crime>(); string sql = ""; int id = 0; // clear exception: EX = null; Input = input; try { // // Do we have an input argument? If so, we do a lookup: // if (input == null) { // // there's no page argument, perhaps user surfed to the page directly? // In this case, nothing to do. // } else { // // Lookup movie(s) based on input, which could be id or a partial name: // if (System.Int32.TryParse(input, out id)) { // lookup movie by movie id: sql = string.Format(@"SELECT top 10 Crimes.IUCR,PrimaryDesc,SecondaryDesc, COUNT(Crimes.IUCR) as numCrimes, ROUND((( CAST(COUNT(Crimes.IUCR) as float ) / (SELECT COUNT(CID) FROM Crimes WHERE Area = {0}))*100 ) , 2) as CrimePercent, ((SUM(CAST(Arrested AS INT))) / (CAST(COUNT(Arrested) AS FLOAT) )) * 100 as ArrestedPercent,AreaName,Areas.Area FROM Crimes RIGHT JOIN Codes ON (Codes.IUCR = Crimes.IUCR) RIGHT JOIN Areas ON (Areas.Area = Crimes.Area) WHERE AREA = {0} GROUP BY Crimes.IUCR,PrimaryDesc,SecondaryDesc,Areas.Area ORDER BY COUNT(Crimes.IUCR) DESC", id); } else { // lookup movie(s) by partial name match: input = input.Replace("'", "''"); sql = string.Format(@"SELECT top 10 Crimes.IUCR,PrimaryDesc,SecondaryDesc, COUNT(Crimes.IUCR) as numCrimes, ROUND((( CAST(COUNT(Crimes.IUCR) as float ) / (SELECT COUNT(CID) FROM Crimes WHERE AreaName LIKE '{0}' ))*100 ) , 2) as CrimePercent, ((SUM(CAST(Arrested AS INT))) / (CAST(COUNT(Arrested) AS FLOAT) )) * 100 as ArrestedPercent,AreaName,Areas.Area FROM Crimes RIGHT JOIN Codes ON (Codes.IUCR = Crimes.IUCR) RIGHT JOIN Areas ON (Areas.Area = Crimes.Area) WHERE AreaName LIKE '{0}' GROUP BY Crimes.IUCR,PrimaryDesc,SecondaryDesc,AreaName,Areas.Area ORDER BY COUNT(Crimes.IUCR) DESC", input); } } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.IUCR = Convert.ToString(row["IUCR"]); m.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); m.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); m.NumCrimes = Convert.ToInt32(row["numCrimes"]); m.CrimePercent = Convert.ToDouble(row["CrimePercent"]); m.ArrestedPercent = Convert.ToDouble(row["ArrestedPercent"]); AreaNameString = Convert.ToString(row["AreaName"]); AreaNumber = Convert.ToString(row["Area"]); crimes.Add(m); } } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; } }
public void OnGet(string input) { List<Models.Crime> crimes = new List<Models.Crime>(); // make input available to web page: Input = input; // clear exception: EX = null; try { // // Do we have an input argument? If so, we do a lookup: // if (input == null) { // // there's no page argument, perhaps user surfed to the page directly? // In this case, nothing to do. // } else { // // Lookup movie(s) based on input, which could be id or a partial name: // int id; string sql; if (System.Int32.TryParse(input, out id)) { // lookup movie by area id: sql = string.Format(@" SELECT TOP 20 Areas.Area, Areas.AreaName, Crimes.Year, Count(*) AS NumCrimes FROM Crimes LEFT JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Year = {0} AND Areas.Area != 0 GROUP BY Areas.Area, Areas.AreaName, Crimes.Year ORDER BY NumCrimes DESC; ", id); } else { // lookup movie(s) by partial name match: input = input.Replace("'", "''"); sql = string.Format(@" SELECT TOP 20 Areas.Area, Areas.AreaName, Crimes.Year, Count(*) AS NumCrimes FROM Crimes LEFT JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Year LIKE '%{0}%' AND Areas.Area != 0 GROUP BY Areas.Area, Areas.AreaName, Crimes.Year ORDER BY NumCrimes DESC; ", input); EX = new Exception("Please enter a year and not a string!"); } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.Area = Convert.ToInt32(row["Area"]); c.AreaName = Convert.ToString(row["AreaName"]); c.Year = Convert.ToInt32(row["Year"]);; c.NumOccured = Convert.ToInt32(row["NumCrimes"]); crimes.Add(c); } }//else } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; NumAreas = crimes.Count; } }
public void OnGet(string input) { List<Models.Crime> Crimes = new List<Models.Crime>(); // make input available to web page: Input = input; // clear exception: EX = null; try { // // Do we have an input argument? If so, we do a lookup: // if (input == null) { // // there's no page argument, perhaps user surfed to the page directly? // In this case, nothing to do. // } else { // // Lookup movie(s) based on input, which could be id or a partial name: // int id; string sql; string sql2; string sql3; //output top 10 crimes in this area of the city! //DISPLAY same info as you did in part 1 // //page should ALSO display the area name and number so its clear // //if user enters an area that doesn't exist, say so! //inner join as before or LEFT join as was found here? if (System.Int32.TryParse(input, out id)) //input was the area ID number { // lookup movie by movie id: sql = string.Format(@" SELECT TOP 10 Crimes.IUCR, PrimaryDesc, SecondaryDesc, Count(*) AS numTimes, Areas.AreaName, Crimes.Area FROM Crimes LEFT JOIN Codes ON Crimes.IUCR = Codes.IUCR INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Area = {0} GROUP BY Crimes.IUCR, PrimaryDesc, SecondaryDesc, Crimes.Area, Areas.AreaName ORDER BY numTimes DESC; ", id); //now for the total number of crimes sql2 = string.Format(@" SELECT Count(*) AS total FROM Crimes WHERE Area = {0}; ", id); sql3 = string.Format(@" SELECT IUCR, Count(Arrested) as Arrested From Crimes Where Arrested=1 AND Area = {0} Group by IUCR; ", id); } else { // lookup movie(s) by partial name match: input = input.Replace("'", "''"); sql = string.Format(@" SELECT TOP 10 Crimes.IUCR, PrimaryDesc, SecondaryDesc, Count(*) AS numTimes, Areas.AreaName, Crimes.Area FROM Crimes LEFT JOIN Codes ON Crimes.IUCR = Codes.IUCR INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Areas.AreaName LIKE '%{0}%' GROUP BY Crimes.IUCR, PrimaryDesc, SecondaryDesc, Crimes.Area, Areas.AreaName ORDER BY numTimes DESC; ", input); //now for the total number of crimes sql2 = string.Format(@" SELECT Count(*) AS total FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Areas.AreaName = '%{0}%'; ", input); //now for the arrested rate sql3 = string.Format(@" SELECT IUCR, Count(Arrested) as Arrested From Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area Where Arrested=1 AND Areas.AreaName LIKE '%{0}%' Group by IUCR; ", input); } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); object result = DataAccessTier.DB.ExecuteScalarQuery(sql2); double totalCrimes = Convert.ToDouble(result); DataSet ds2 = DataAccessTier.DB.ExecuteNonScalarQuery(sql3); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime m = new Models.Crime(); m.IUCR = Convert.ToString(row["IUCR"]); m.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); m.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); m.numTimes = Convert.ToInt32(row["numTimes"]); m.percentChicago = m.numTimes/totalCrimes; arName = Convert.ToString(row["AreaName"]); arNum = Convert.ToInt32(row["Area"]); Crimes.Add(m); } foreach (DataRow row in ds2.Tables["TABLE"].Rows) //this ds has all the crimes IUCRs and arrest numbers { string iucr = Convert.ToString(row["IUCR"]); double arrested = Convert.ToDouble(row["Arrested"]); foreach(Models.Crime c in Crimes) //search if this iucr is in our top 10 crimes list { if(c.IUCR == iucr){ //if the iucr is found in our top 10, assign its arrested number c.arrestRate = ((arrested/c.numTimes) *100); } } } //end for each } }//end try stmt catch(Exception ex) { EX = ex; } finally { CrimeList = Crimes; } }
public void OnGet(string input) { List<Models.Crime> crimes = new List<Models.Crime>(); // make input available to web page: Input = input; // clear exception: EX = null; try { // // Do we have an input argument? If so, we do a lookup: // if (input == null) { // // there's no page argument, perhaps user surfed to the page directly? // In this case, nothing to do. // } else { // // Lookup movie(s) based on input, which could be id or a partial name: // int id; string sql; string sql1 = null; // string sql2 = null; if (System.Int32.TryParse(input, out id)) { // lookup movie by movie id: sql = string.Format(@" SELECT top 10 Codes.IUCR,Codes.PrimaryDesc,Codes.SecondaryDesc, COUNT(*) AS TotalNumOfCrimes, ROUND((CONVERT(float,COUNT(*))*100.0/(SELECT COUNT(*) FROM Crimes)),2) AS percentOfCrime,ROUND(AVG(CONVERT(float,Arrested))*100.0,2) AS PercentageOfArrest FROM Crimes INNER JOIN Codes ON Crimes.IUCR = Codes.IUCR WHERE Area = {0} GROUP BY Codes.IUCR,Codes.PrimaryDesc,Codes.SecondaryDesc ORDER BY TotalNumOfCrimes DESC; ", id); sql1 = string.Format(@" SELECT Area, AreaName FROM Areas WHERE Area = {0};",id); } else { // lookup movie(s) by partial name match: input = input.Replace("'", "''"); sql = string.Format(@" SELECT top 10 Codes.IUCR,Codes.PrimaryDesc,Codes.SecondaryDesc, COUNT(*) AS TotalNumOfCrimes, ROUND((CONVERT(float,COUNT(*))*100.0/(SELECT COUNT(*) FROM Crimes)),2) AS percentOfCrime,ROUND(AVG(CONVERT(float,Arrested))*100.0,2) AS PercentageOfArrest FROM Crimes INNER JOIN Codes ON Crimes.IUCR = Codes.IUCR INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE AreaName LIKE '%{0}%' GROUP BY Codes.IUCR,Codes.PrimaryDesc,Codes.SecondaryDesc ORDER BY TotalNumOfCrimes DESC; ", input); sql1 = string.Format(@" SELECT Area, AreaName FROM Areas WHERE AreaName LIKE '%{0}%';",input); } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"])+" "+ Convert.ToString(row["SecondaryDesc"]);; //c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.TotalNumOfCrimes = Convert.ToInt32(row["TotalNumOfCrimes"]); c.PercentOfCrime = Convert.ToDouble(row["PercentOfCrime"]); c.PercentageOfArrest = Convert.ToDouble(row["PercentageOfArrest"]); crimes.Add(c); } DataSet ds1 = DataAccessTier.DB.ExecuteNonScalarQuery(sql1); foreach (DataRow row1 in ds1.Tables["TABLE"].Rows) { AreaNumber = Convert.ToInt32(row1["Area"]); AreaName = Convert.ToString(row1["AreaName"]); } }//else } catch(Exception ex) { EX = ex; } finally { CrimeList = crimes; NumCrimes = crimes.Count; } }
public void OnGet(string input) { List<Models.Crime> crimeList = new List<Models.Crime>(); // clear exception: EX = null; // initialize messages ErrorMsg = " "; NoResultMsg = " "; // make input available to web page: Input = input; try { // No input if (input == null) { // there's no page argument, perhaps user surfed to the page directly? // In this case, nothing to do. NoResultMsg = "Search results are empty."; } // User input exists else { // Lookup crime(s) based on input, which could be area number or area name: int id; string sql; // lookup crime by id: if (System.Int32.TryParse(input, out id)) { sql = string.Format(@" DECLARE @totalNumCrimes INT = (SELECT Count(*) FROM Crimes WHERE Crimes.Area = {0}) SELECT TOP 10 Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, COUNT(Crimes.IUCR) AS TotalNumTimes, Areas.Area, AreaName, CONVERT(decimal(3,2), ( CONVERT( float, COUNT(Crimes.IUCR))/@totalNumCrimes ) * 100 ) AS CrimePercentage, CONVERT(decimal(5,2), ( CONVERT( float, SUM( CONVERT(float, Crimes.Arrested) ) ) / CONVERT( float, Count(Crimes.IUCR)) ) * 100 ) AS ArrestPercentage FROM Crimes FULL OUTER JOIN Codes ON Codes.IUCR = Crimes.IUCR FULL OUTER JOIN Areas ON Areas.Area = Crimes.Area WHERE Crimes.Area = {0} GROUP BY Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, Areas.Area, AreaName ORDER BY TotalNumTimes desc; ", id); } // lookup crime by name: else { sql = string.Format(@" DECLARE @totalNumCrimes INT = (SELECT Count(*) FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Areas.AreaName = '{0}') SELECT TOP 10 Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, COUNT(Crimes.IUCR) AS TotalNumTimes, Areas.Area, AreaName, CONVERT(decimal(3,2), ( CONVERT( float, COUNT(Crimes.IUCR))/@totalNumCrimes ) * 100 ) AS CrimePercentage, CONVERT(decimal(5,2), ( CONVERT( float, SUM( CONVERT(float, Crimes.Arrested) ) ) / CONVERT( float, Count(Crimes.IUCR)) ) * 100 ) AS ArrestPercentage FROM Crimes FULL OUTER JOIN Codes ON Codes.IUCR = Crimes.IUCR FULL OUTER JOIN Areas ON Areas.Area = Crimes.Area WHERE Areas.AreaName = '{0}' GROUP BY Crimes.IUCR, Codes.PrimaryDesc, Codes.SecondaryDesc, Areas.Area, AreaName ORDER BY TotalNumTimes desc; ", input); } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); // check if query results are empty if(ds.Tables[0].Rows.Count == 0) { NoResultMsg = "Search results are empty."; } else { foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.NumTimes = Convert.ToInt32(row["TotalNumTimes"]); c.CrimePercentage = Convert.ToSingle(row["CrimePercentage"]); c.ArrestPercentage = Convert.ToSingle(row["ArrestPercentage"]); c.AreaName = Convert.ToString(row["AreaName"]); c.AreaNum = Convert.ToInt32(row["Area"]); crimeList.Add(c); } } } } // end try catch(Exception ex) { EX = ex; ErrorMsg = "Something went wrong. Could not find search results. Please try again."; } finally { CrimeAreaList = crimeList; } } // end onGet()
public void OnGet(string input) { List <Models.Crime> crimes = new List <Models.Crime>(); // make input available to web page: Input = input; //clear exception: EX = null; try { if (input == null) { //nothing to do. } else { int id; string sql; if (System.Int32.TryParse(input, out id)) { //lookup area by area id sql = string.Format(@" SELECT TOP 10 Crimes.IUCR, PrimaryDesc, SecondaryDesc, Count(CID) AS NumOccur, ROUND(CAST(COUNT(CID) AS FLOAT)/(SELECT COUNT(CID) FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Area = {0})*100, 2) AS 'Crime %', ROUND(CAST(COUNT(CASE WHEN Arrested = 1 THEN 1 END)AS FLOAT)/COUNT(Crimes.IUCR)*100, 2) AS 'Arrested %' FROM Crimes INNER JOIN codes ON Crimes.IUCR = Codes.IUCR INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE Crimes.Area = {0} GROUP BY Crimes.IUCR, PrimaryDesc, SecondaryDesc ORDER BY numOccur DESC; ", id); } else { //lookup area by partial name match input = input.Replace("'", "''"); sql = string.Format(@" SELECT TOP 10 Crimes.IUCR, PrimaryDesc, SecondaryDesc, Count(CID) AS NumOccur, ROUND(CAST(COUNT(CID) AS FLOAT)/(SELECT COUNT(CID) FROM Crimes INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE AreaName LIKE '%Rogers%')*100, 2) AS 'Crime %', ROUND(CAST(COUNT(CASE WHEN Arrested = 1 THEN 1 END)AS FLOAT)/COUNT(Crimes.IUCR)*100, 2) AS 'Arrested %' FROM Crimes INNER JOIN codes ON Crimes.IUCR = Codes.IUCR INNER JOIN Areas ON Crimes.Area = Areas.Area WHERE AreaName LIKE '%{0}%' GROUP BY Crimes.IUCR, PrimaryDesc, SecondaryDesc ORDER BY numOccur DESC; ", input); } DataSet ds = DataAccessTier.DB.ExecuteNonScalarQuery(sql); if (ds.Tables[0].Rows.Count == 0) { throw new Exception("Area not found"); } foreach (DataRow row in ds.Tables["TABLE"].Rows) { Models.Crime c = new Models.Crime(); c.IUCR = Convert.ToString(row["IUCR"]); c.PrimaryDesc = Convert.ToString(row["PrimaryDesc"]); c.SecondaryDesc = Convert.ToString(row["SecondaryDesc"]); c.NumOccur = Convert.ToInt32(row["NumOccur"]); c.CrimePer = Convert.ToDouble(row["Crime %"]); c.ArrestedPer = Convert.ToDouble(row["Arrested %"]); crimes.Add(c); } } } catch (Exception ex) { EX = ex; } finally { CrimeList = crimes; } }