Showing posts with label Delete. Show all posts
Showing posts with label Delete. Show all posts

Friday, July 3, 2015

Entity FrameWork With MVC5 Part 4 (Delete)

we earlier saw how easy and efficient it was to add a record and read a record .
same way we can do delete operation

Note : we first find the id of the record to be deleted and then hit delete . 

so below are the action result to delete the records (get and post methods)



[HttpGet]
        public ActionResult Delete(int id)
        {
            ProjectWBSContext pwbs = new ProjectWBSContext();
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Projects projects = pwbs.Projects.Find(id);
            if (projects == null)
            {
                return HttpNotFound();
            }
            return View(projects);
        }
        ///Project/Delete/2
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
          

                ProjectWBSContext pwbs = new ProjectWBSContext();
                Projects projects = pwbs.Projects.Find(id);

                pwbs.Projects.Remove(projects);
                pwbs.SaveChanges();

                return RedirectToAction("Index");
         

           


        }

Create a view and then hit run . you will see the UI to delete the records 



Tuesday, January 13, 2015

Deleting MongoDB Records using Gridview

So far we have seen select and insert Operations .
we now see how we can perform delete operation.
Place a check box control in the gridview like below



<asp:GridView ID="gvShowTasks" runat="server" CssClass="table table-striped table-bordered table-hover" OnRowCancelingEdit="gvShowTasks_RowCancelingEdit" OnRowEditing="gvShowTasks_RowEditing"  DataKeyNames="Id" OnRowUpdating="gvShowTasks_RowUpdating">
                   <Columns>
                       <asp:TemplateField>
                           <ItemTemplate>
                               <asp:CheckBox ID="chk" runat="server" />
                           </ItemTemplate>
                       </asp:TemplateField>
                       <asp:CommandField ShowEditButton="true" />
                   </Columns>
               </asp:GridView>


on the Code Behind Place the below code in the Delete button click event



protected void btnDelete_Click(object sender, EventArgs e)
    {
        MongoClient monClient = new MongoClient("mongodb://localhost");
        MongoServer ms = monClient.GetServer();
        ms.Connect();
        MongoDatabase db = ms.GetDatabase("testdb");
        MongoCollection collection = db.GetCollection<Tasks>("Tasks");
        Tasks tsk = new Tasks();

        foreach (GridViewRow gvr in gvShowTasks.Rows)
        {
            var rowIndex = gvr.RowIndex;
            var TaskID = gvShowTasks.DataKeys[rowIndex].Values["Id"].ToString();// capturing object id for deletion
            var rowVal = gvShowTasks.Rows[rowIndex].Cells[3].Text;
            CheckBox chk = (CheckBox)gvr.FindControl("chk");
          if (chk.Checked)
          {
              ObjectId bsonId=(ObjectId.Parse(TaskID));//converting string to object id
              IMongoQuery query = Query.EQ("_id", bsonId);
              collection.Remove(query);
             

          }
           
           
        }
        var ShowTasks = collection.FindAllAs<Tasks>().ToList<Tasks>(); //Queries the tasks collection and Converts to List           
        gvShowTasks.DataSource = ShowTasks;//Set the List as Data source 
        gvShowTasks.DataBind();//Binds the Grid
    }

 Run the solution and there you go .. whichever row you have selected , on click of delete button the same row will be deleted . you can also customise your delete query in the statement
  IMongoQuery query = Query.EQ("_id", bsonId);