// Get users from db and display in gridview
        protected void getUsers()
        {
            // Connect to db
            var conn = new a2Entities();

            // Select users from db using d as a variable (LINQ format)
            var users = from d in conn.AspNetUsers select d;

            // Pass query result to gridview
            usersGrid.DataSource = users.ToList();
            usersGrid.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // Only do this the first time
            if (IsPostBack == false)
            {
                // Connect to db
                var conn = new a2Entities();

                // Build asset related dropdown
                // http://stackoverflow.com/questions/554649/how-to-bind-linq-data-to-dropdownlist
                using (var conn2 = new a2Entities())
                {
                    assets.DataSource     = (from a in conn.assets orderby a.createdDate select new { a.callSign, a.Id }).ToList();
                    assets.DataTextField  = Convert.ToString("callSign");
                    assets.DataValueField = Convert.ToString("Id");
                    assets.DataBind();
                }

                assets.Items.Insert(0, new ListItem(" --Select-- ", ""));

                // Check if there is parameter being passed in url
                if (!String.IsNullOrEmpty(Request.QueryString["logId"]))
                {
                    // Store paramater from url in variable
                    Int32 logId = Convert.ToInt32(Request.QueryString["logId"]);

                    // Check that the provided id is valid
                    // http://stackoverflow.com/questions/4132514/how-to-know-if-my-linq-query-returns-null
                    var queryValidBool = (from l in conn.logs where l.Id == logId select l).ToList().Any();

                    // If nothing is returned in the query return user to table of avialable logs
                    if (queryValidBool == false)
                    {
                        Response.Redirect(ResolveUrl("~/private/logs.aspx"));
                    }

                    // Look for selected log in db
                    var logObj = (from l in conn.logs where l.Id == logId select l).FirstOrDefault();

                    // Render page as log edit
                    add_log_header.Text = "Edit Log: " + logObj.title;
                    add_log_button.Text = "Save Changes";

                    // Populate form with data
                    title.Text           = logObj.title;
                    notes.Text           = logObj.notes;
                    assets.SelectedValue = Convert.ToString(logObj.assetId);
                }
            }
        }
        // Get assets from db and display in gridview
        protected void getAssets()
        {
            // Connect to db
            var conn = new a2Entities();

            // Select assets from db using d as a variable (LINQ format)
            var assets = from d in conn.assets orderby d.Id descending select d;

            // Count returned query values
            var assetsCount = assets.Count();

            // Pass query result to gridview
            assetsGrid.DataSource = assets.ToList();
            assetsGrid.DataBind();

            // Display messege if there are no assets
            if (assetsCount == 0)
            {
                assetsMsg.Text = "There are no assets to display.";
            }
        }
        // Delete asset from gridview
        protected void assetsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Get selected row
            Int32 rowIndex = e.RowIndex;

            // Find the assets's id
            Int32 assetId = Convert.ToInt32(assetsGrid.DataKeys[rowIndex].Value);

            // Connect to db
            var conn = new a2Entities();

            // Delete the selected asset
            asset d = new asset();

            d.Id = assetId;
            conn.assets.Attach(d);
            conn.assets.Remove(d);
            conn.SaveChanges();

            // Reload gridview
            getAssets();
        }
        // Delete user from gridview
        protected void usersGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Get selected row
            Int32 rowIndex = e.RowIndex;

            // Find the user's id
            String userId = Convert.ToString(usersGrid.DataKeys[rowIndex].Value);

            // Connect to db
            var conn = new a2Entities();

            // Delete the selected user
            AspNetUser d = new AspNetUser();

            d.Id = userId;
            conn.AspNetUsers.Attach(d);
            conn.AspNetUsers.Remove(d);
            conn.SaveChanges();

            // Reload gridview
            getUsers();
        }
Example #6
0
        // Get assets from db and display in gridview
        protected void getData()
        {
            // Connect to db
            var conn = new a2Entities();

            // Select recent table additions for dashboard.aspx view
            // http://stackoverflow.com/questions/4872946/linq-query-to-select-top-five
            var whereCurDateTime = DateTime.Now.AddDays(-7);

            // Select assets from db using (LINQ format)
            // http://stackoverflow.com/questions/4872946/linq-query-to-select-top-five
            var assets = (from a in conn.assets orderby a.Id descending where (a.createdDate > whereCurDateTime) select a).Take(10);

            // Count returned query values
            var assetsCount = assets.Count();

            // Pass query result to gridview
            recentAssetsGrid.DataSource = assets.ToList();
            recentAssetsGrid.DataBind();

            var logs = (from l in conn.logs orderby l.Id descending where (l.createdDate > whereCurDateTime) select l).Take(10);

            var logsCount = logs.Count();

            // Pass query result to gridview
            recentLogsGrid.DataSource = logs.ToList();
            recentLogsGrid.DataBind();

            // Display messege if there are no recent accets or logs
            if (assetsCount == 0)
            {
                recentAssetsMsg.Text = "There are no recent assets.";
            }

            if (logsCount == 0)
            {
                recentLogsMsg.Text = "There are no recent logs.";
            }
        }
        // Delete log from gridview
        protected void logsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Get selected row
            Int32 rowIndex = e.RowIndex;

            // Find the logs's id
            Int32 logId = Convert.ToInt32(logsGrid.DataKeys[rowIndex].Value);

            // Connect to db
            var conn = new a2Entities();

            // Delete the selected log
            log d = new log();

            d.Id = logId;
            conn.logs.Attach(d);
            conn.logs.Remove(d);
            conn.SaveChanges();

            // Reload gridview
            getLogs();
        }
        protected void Save_Log(object sender, EventArgs e)
        {
            Int32 logId = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["logId"]))
            {
                logId = Convert.ToInt32(Request.QueryString["logId"]);
            }

            var conn = new a2Entities();

            log l = new log();

            // Get current user id
            // http://stackoverflow.com/questions/20925822/asp-mvc5-identity-how-to-get-current-applicationuser
            string userId = User.Identity.GetUserId();
            var    user   = (new a2Entities()).AspNetUsers.FirstOrDefault(u => u.Id == userId);

            // Attach values to new object
            l.authorId = Convert.ToString(user);
            l.title    = title.Text;
            l.notes    = notes.Text;

            if (!String.IsNullOrEmpty(assets.SelectedValue))
            {
                l.assetId = Convert.ToInt32(assets.SelectedValue);
            }

            // Get asset related data
            if (!String.IsNullOrEmpty(l.assetId.ToString()))
            {
                var assetObj = (from a2 in conn.assets where a2.Id == l.assetId select a2).FirstOrDefault();
                l.assetCallSign = assetObj.callSign;
            }

            // Save logObj to db
            if (logId == 0)
            {
                // Get current datetime in EST format
                // http://stackoverflow.com/questions/5997570/how-to-convert-datetime-to-eastern-time
                var          timeUtc     = DateTime.UtcNow;
                TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
                l.createdDate = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

                conn.logs.Add(l);
            }
            // Update log
            else
            {
                l.Id = logId;
                conn.logs.Attach(l);
                conn.Entry(l).State = System.Data.Entity.EntityState.Modified;
                // Make sure created datetime does NOT get updated
                conn.Entry(l).Property(o => o.createdDate).IsModified = false;
            }

            // Save the log changes
            conn.SaveChanges();

            // Redirect to logs
            Response.Redirect(ResolveUrl("~/private/logs.aspx"));
        }
Example #9
0
        protected void Save_Asset(object sender, EventArgs e)
        {
            // Determine if asset is being updated or added
            Int32 assetId = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["assetId"]))
            {
                assetId = Convert.ToInt32(Request.QueryString["assetId"]);
            }

            var conn = new a2Entities();

            asset a = new asset();

            // Get current user id
            // http://stackoverflow.com/questions/20925822/asp-mvc5-identity-how-to-get-current-applicationuser
            string userId = User.Identity.GetUserId();
            var    user   = (new a2Entities()).AspNetUsers.FirstOrDefault(s => s.Id == userId);

            // Attach values to new object
            a.authorId = Convert.ToString(user);
            a.callSign = call_sign.Text;

            if (!String.IsNullOrEmpty(year.SelectedValue))
            {
                a.year = Convert.ToInt32(year.SelectedValue);
            }

            a.brand = brand.Text;
            a.model = model.Text;
            a.vin   = vin.Text;

            // Make sure there is a value to convert before converting to avoid errors
            if (!String.IsNullOrEmpty(hours.Text))
            {
                a.hours = Convert.ToInt32(hours.Text);
            }

            a.odometerUnit = odometer_unit.SelectedValue;

            if (!String.IsNullOrEmpty(odometer.Text))
            {
                a.odometer = Convert.ToInt32(odometer.Text);
            }

            if (!String.IsNullOrEmpty(purchased_date.Text))
            {
                a.purchasedDate = DateTime.ParseExact(purchased_date.Text, "yyyy-MM-dd", null);
            }

            if (!String.IsNullOrEmpty(price_paid.Text))
            {
                a.pricePaid = Convert.ToDecimal(price_paid.Text);
            }

            a.notes = notes.Text;

            // Save assetObj to db
            if (assetId == 0)
            {
                // Get current datetime in EST format
                // http://stackoverflow.com/questions/5997570/how-to-convert-datetime-to-eastern-time
                var          timeUtc     = DateTime.UtcNow;
                TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
                a.createdDate = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

                conn.assets.Add(a);
            }
            // Update asset
            else
            {
                a.Id = assetId;
                conn.assets.Attach(a);
                conn.Entry(a).State = System.Data.Entity.EntityState.Modified;
                // Make sure created datetime does NOT get updated
                conn.Entry(a).Property(o => o.createdDate).IsModified = false;
            }

            // Save the asset
            conn.SaveChanges();

            // Redirect to assets
            Response.Redirect(ResolveUrl("~/private/assets.aspx"));
        }
Example #10
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Generate dropdown for year
                for (int i = 1950; i < DateTime.Now.Year + 1; i++)
                {
                    year.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                // Check if there is parameter being passed in url
                if (!String.IsNullOrEmpty(Request.QueryString["assetId"]))
                {
                    // Store paramater from url in variable
                    Int32 assetId = Convert.ToInt32(Request.QueryString["assetId"]);

                    // Connect to db
                    var conn = new a2Entities();

                    // Check that the provided id is valid
                    // http://stackoverflow.com/questions/4132514/how-to-know-if-my-linq-query-returns-null
                    var queryValidBool = (from a in conn.assets where a.Id == assetId select a).ToList().Any();

                    // If nothing is returned in the query return user to table of avialable assets
                    if (queryValidBool == false)
                    {
                        Response.Redirect(ResolveUrl("~/private/assets.aspx"));
                    }

                    // Look for selected asset in db
                    var assetObj = (from a in conn.assets where a.Id == assetId select a).FirstOrDefault();

                    // Render page as asset edit
                    add_asset_header.Text = "Edit Asset: " + assetObj.callSign;
                    add_asset_button.Text = "Save Changes";

                    // Populate form with data
                    call_sign.Text              = assetObj.callSign;
                    year.SelectedValue          = Convert.ToString(assetObj.year);
                    brand.Text                  = assetObj.brand;
                    model.Text                  = assetObj.model;
                    vin.Text                    = assetObj.vin;
                    hours.Text                  = Convert.ToString(assetObj.hours);
                    odometer_unit.SelectedValue = Convert.ToString(assetObj.odometerUnit);

                    if (assetObj.odometer != null)
                    {
                        odometer.Text = Convert.ToString(assetObj.odometer);
                    }

                    // Check if there is a value before filling input to avoid auto filling of a default value caused by the convert method
                    if (assetObj.purchasedDate != null)
                    {
                        purchased_date.Text = Convert.ToDateTime(assetObj.purchasedDate).ToString("yyyy-MM-dd");
                    }

                    // Format string into money if value is present
                    // http://stackoverflow.com/questions/10615405/how-to-format-string-to-money
                    if (assetObj.pricePaid != null)
                    {
                        price_paid.Text = string.Format("{0:#.00}", Convert.ToDecimal(assetObj.pricePaid));
                    }

                    notes.Text = assetObj.notes;
                }
            }
        }