// 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();
        }
示例#2
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"));
        }