//View a Post
        public ActionResult Post(long? id)
        {
            var db = new PostsDataContext();
            var post = db.Posts.Find(id);

            return View();
        }
        public ActionResult Create(Post post, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                //Save Posts the Databse
                var db = new PostsDataContext();
                db.Posts.Add(post);
                db.SaveChanges();
            }
            DataSet ds = new DataSet();
            var requestFile = Request.Files["file"];
            var requestFileUpload = Request.Files["FileUpload"];
            if (file == null)
            {
                return null;
            }
            else if (requestFile.ContentLength > 0)
            {
                string fileExtension = System.IO.Path.GetExtension(requestFile.FileName);

                if (fileExtension == ".xls" || fileExtension == ".xlsx")
                {
                    string fileLocation = Server.MapPath("~/Content/uploads/") + requestFile.FileName;
                    try
                    {
                        if (System.IO.File.Exists(fileLocation))
                        {
                            System.IO.File.Delete(fileLocation);
                        }
                        else
                        {
                            Debug.WriteLine("File does not exist.");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    requestFile.SaveAs(fileLocation);
                    string excelConnectionString = string.Empty;

                    //connect to the Database
                    excelConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\asp_blog.Models.PostsDataContext.mdf;Integrated Security=True";

                    //connect to Excel xls file format
                    if (fileExtension == ".xls")
                    {
                        excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                    }
                    else if (fileExtension == ".xlsx")
                    {
                        excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                    }

                    //Create Connection to Excel via OleDbConnect
                    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                    excelConnection.Open();
                    DataTable dt = new DataTable();

                    dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return null;
                    }

                    String[] excelSheets = new String[dt.Rows.Count];
                    int t = 0;

                    //Save Excel Data in This Temp File
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[t] = row["TABLE_NAME"].ToString();
                        t++;
                    }

                    OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);

                    string query = string.Format("Select * FROM [{0}]", excelSheets[0]);

                    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                    {
                        dataAdapter.Fill(ds);
                    }
                }

                if (fileExtension.ToLower().Equals(".xml"))
                {
                    string fileLocation = Server.MapPath("~/Content/uploads/") + requestFileUpload.FileName;

                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }

                    requestFileUpload.SaveAs(fileLocation);
                    XmlTextReader xmlreader = new XmlTextReader(fileLocation);
                    ds.ReadXml(xmlreader);
                    xmlreader.Close();
                }

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string conn = ConfigurationManager.ConnectionStrings["asp_blog.Models.PostsDataContext"].ConnectionString;
                    SqlConnection con = new SqlConnection(conn);
                    string query = "Insert into Posts(Title,Description) Values('" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "')";
                    con.Open();
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

            return RedirectToAction("Index");
        }
        public ActionResult DeleteConfirmed(long? id)
        {
            //Delete a Record in the database
            var db = new PostsDataContext();
            Post post = db.Posts.Find(id);
            db.Posts.Remove(post);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        //Delete a Post
        public ActionResult Delete(long? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
            }

            var db = new PostsDataContext();
            Post post = db.Posts.Find(id);
            if (post == null)
            {
                return HttpNotFound();
            }

            return View(post);
        }
 public ActionResult Edit([Bind(Include = "Id,Title,Description")] Post post)
 {
     if (ModelState.IsValid)
     {
         var db = new PostsDataContext();
         db.Entry(post).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(post);
 }
 // GET: Posts
 public ActionResult Index()
 {
     var db = new PostsDataContext();
     var posts = db.Posts.ToArray();
     return View(posts);
 }