/// <summary>
        /// Inserting new rows into database
        /// </summary>
        /// <param name="rows"></param>
        protected void InsertRows(IEnumerable <Car> rows)
        {
            using (SampleDatabaseEntities db = new SampleDatabaseEntities())
            {
                try
                {
                    foreach (var item in rows)
                    {
                        CarsTable carsTable = new CarsTable();
                        carsTable.Factory   = item.Factory;
                        carsTable.Model     = item.Model;
                        carsTable.Warranty  = item.Warranty;
                        carsTable.ModelYear = DateTime.Now;
                        db.CarsTables.Add(carsTable);
                    }

                    db.SaveChanges();
                    StatusLabel.Text = "Import success!";
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Problem with import: " + ex.Message;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Feeding grid with db rows
        /// </summary>
        /// <returns></returns>
        public IQueryable <CarsTable> carsGrid_GetData()
        {
            SampleDatabaseEntities db = new SampleDatabaseEntities();
            var allRows = db.CarsTables.Select(x => x);

            return(allRows);
        }
        public IQueryable <CarsTable> carsGrid_GetData([QueryString] string factory)
        {
            SampleDatabaseEntities db = new SampleDatabaseEntities();
            var allRows = db.CarsTables.Select(x => x).Where(x => x.Factory == factory);

            //checking if button clicked to avoid message on page load
            if (allRows.Count() == 0 && ClientQueryString.Count() > 0)
            {
                LabelNoRecords.Text = "<br />No records found";
            }

            return(allRows);
        }
Esempio n. 4
0
        public void addCar_InsertItem()
        {
            var item = new CarsTable();

            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                using (SampleDatabaseEntities db = new SampleDatabaseEntities())
                {
                    db.CarsTables.Add(item);
                    db.SaveChanges();
                }
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Deleting item
 /// </summary>
 /// <param name="Id"></param>
 public void carsGrid_DeleteItem(int Id)
 {
     using (SampleDatabaseEntities db = new SampleDatabaseEntities())
     {
         var item = new CarsTable {
             Id = Id
         };
         db.Entry(item).State = EntityState.Deleted;
         try
         {
             db.SaveChanges();
         }
         catch (DbUpdateConcurrencyException)
         {
             ModelState.AddModelError("",
                                      String.Format("Delete issue: item id {0} not found in database.", Id));
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Update item
        /// </summary>
        /// <param name="Id"></param>
        public void carsGrid_UpdateItem(int Id)
        {
            using (SampleDatabaseEntities db = new SampleDatabaseEntities())
            {
                CarsTable item = null;
                item = db.CarsTables.Find(Id);
                if (item == null)
                {
                    ModelState.AddModelError("",
                                             String.Format("Update issue: item id {0} not found in database.", Id));
                    return;
                }

                TryUpdateModel(item);

                if (ModelState.IsValid)
                {
                    db.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Gathering db content into list of objects
        /// </summary>
        protected void GetRows()
        {
            List <Car> rows = new List <Car>();

            using (SampleDatabaseEntities db = new SampleDatabaseEntities())
            {
                try
                {
                    foreach (var item in db.CarsTables)
                    {
                        rows.Add(new Car(item.Factory, item.Model, (int)item.Warranty, (DateTime)item.ModelYear));
                    }
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Problem with db export: " + ex.Message;
                }

                Serialize(rows);
            }
        }
Esempio n. 8
0
        protected void Page_Load(object sender, EventArgs e)
        {
            List <CarsTable> allRows = new List <CarsTable>();

            //gathering records
            using (SampleDatabaseEntities db = new SampleDatabaseEntities())
            {
                allRows = db.CarsTables.Select(x => x).ToList();
            }

            //feeding WebGrid
            WebGrid webGrid = new WebGrid(allRows, canPage: true, defaultSort: "Id", canSort: true, rowsPerPage: 5);

            dbRowsControl.Text = webGrid.GetHtml(
                columns: webGrid.Columns(
                    webGrid.Column("Id", "ID"),
                    webGrid.Column("Factory", "Company"),
                    webGrid.Column("Model", "Model"),
                    webGrid.Column("Warranty", "Warranty (years)"),
                    webGrid.Column("ModelYear", "Model Year", format: (item) => item.ModelYear.ToString("MM/yyyy"))
                    )
                ).ToString();
        }