public JsonResult GetAjaxData(JQueryDataTableParamModel param) { using (var e = new ExampleEntities()) { var totalRowsCount = new System.Data.Objects.ObjectParameter("TotalRowsCount", typeof(int)); var filteredRowsCount = new System.Data.Objects.ObjectParameter("FilteredRowsCount", typeof(int)); var data = e.pr_SearchPerson(param.sSearch, Convert.ToInt32(Request["iSortCol_0"]), Request["sSortDir_0"], param.iDisplayStart, param.iDisplayStart + param.iDisplayLength, totalRowsCount, filteredRowsCount); var aaData = data.Select(d => new string[] { d.FirstName, d.LastName, d.Nationality, d.DateOfBirth.Value.ToString("dd MMM yyyy") }).ToArray(); return(Json(new { sEcho = param.sEcho, aaData = aaData, iTotalRecords = Convert.ToInt32(totalRowsCount.Value), iTotalDisplayRecords = Convert.ToInt32(filteredRowsCount.Value) }, JsonRequestBehavior.AllowGet)); } }
public PartialViewResult GetRowDetail(string firstName, string lastName) { using (var e = new ExampleEntities()) { var data = e.pr_GetPersonDetail(firstName, lastName); var detail = data.FirstOrDefault(); return(PartialView("RowDetail", new RowDetailModel() { DateAdded = detail.DateAdded, MobileTel = detail.MobileTel, HomeTel = detail.HomeTel, EmailAddress = detail.EmailAddress })); } }
public ActionResult AddToICalendar(string downloadFileName, int eventId) { // replace db with however you call your Entity Framework or however you get your data. // In this example, we have an Events collection in our model. using ( var db = new ExampleEntities( ConfigurationManager.ConnectionStrings[YourConnectionString].ConnectionString)) { // Alternatively, you may use db.Events.Find(eventId) if this fits better. var demoEvent = db.Events.Single(getEvent => getEvent.ID == eventId); var icalStringbuilder = new StringBuilder(); icalStringbuilder.AppendLine("BEGIN:VCALENDAR"); icalStringbuilder.AppendLine("PRODID:-//MyTestProject//EN"); icalStringbuilder.AppendLine("VERSION:2.0"); icalStringbuilder.AppendLine("BEGIN:VEVENT"); icalStringbuilder.AppendLine("SUMMARY;LANGUAGE=en-us:" + demoEvent.EventName); icalStringbuilder.AppendLine("CLASS:PUBLIC"); icalStringbuilder.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow)); icalStringbuilder.AppendLine("DESCRIPTION:" + demoEvent.Description); icalStringbuilder.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", demoEvent.StartDateTime)); icalStringbuilder.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", demoEvent.EndDateTime)); icalStringbuilder.AppendLine("SEQUENCE:0"); icalStringbuilder.AppendLine("UID:" + Guid.NewGuid()); icalStringbuilder.AppendLine( string.Format( "LOCATION:{0}\\, {1}\\, {2}\\, {3} {4}", evt.LocationName, evt.Address, evt.City, evt.State, evt.ZipCode).Trim()); icalStringbuilder.AppendLine("END:VEVENT"); icalStringbuilder.AppendLine("END:VCALENDAR"); var bytes = Encoding.UTF8.GetBytes(icalStringbuilder.ToString()); return(this.File(bytes, "text/calendar", downloadFileName)); } }
public ActionResult ExcelUpload(HttpPostedFileBase postedFile) { string filePath = string.Empty; if (postedFile != null) { string path = Server.MapPath("~/Uploads/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //identifying file extension to decide which Excel version to use as connection string. The string info is in the Web.Config file filePath = path + Path.GetFileName(postedFile.FileName); string extension = Path.GetExtension(postedFile.FileName); postedFile.SaveAs(filePath); string conString = string.Empty; switch (extension) { case ".xls": //Connection string for Excel 97-03. conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString; break; case ".xlsx": //Connection string for Excel 07 and above. conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString; break; } DataTable dt = new DataTable(); conString = string.Format(conString, filePath); using (OleDbConnection connExcel = new OleDbConnection(conString)) { using (OleDbCommand cmdExcel = new OleDbCommand()) { using (OleDbDataAdapter odaExcel = new OleDbDataAdapter()) { using (ExampleEntities db = new ExampleEntities()) { cmdExcel.Connection = connExcel; //Get the name of First Sheet. connExcel.Open(); DataTable dtExcelSchema; dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); connExcel.Close(); //Read Data from First Sheet. connExcel.Open(); cmdExcel.CommandText = "SELECT * From [" + sheetName + "] "; odaExcel.SelectCommand = cmdExcel; odaExcel.Fill(dt); // This part required for deleting empty columns. Excel file automatically inserts empty rows, if havent been removed, they will be added as null data. for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["Name"].ToString() == null || dt.Rows[i]["Name"].ToString() == "") { dt.Rows[i].Delete(); } } dt.AcceptChanges(); int newRowCount = dt.Rows.Count; for (int i = 0; i < newRowCount; i++) { var name = dt.Rows[i]["Name"].ToString(); var info = dt.Rows[i]["Info"].ToString(); var price = dt.Rows[i]["Price"]; if (price.ToString() == "") { price = 0; } else { price = Convert.ToDouble(price); } var status = dt.Rows[i]["Status"]; if (status.ToString() == "") { status = 0; } else { status = Convert.ToInt32(status); } var registerDate = DateTime.Now; var stock = dt.Rows[i]["Stock"]; if (stock.ToString() != "") { stock = Convert.ToInt32(dt.Rows[i]["Stock"]); } Product _product = new Product() { Name = name, Info = info, RegisterDate = registerDate, Price = Convert.ToDouble(price), Status = Convert.ToInt32(status), Stock = Convert.ToInt32(stock) }; db.Product.Add(_product); db.SaveChanges(); } } connExcel.Close(); } } } } return(RedirectToAction("Index")); }
public ActionResult Index() { ExampleEntities db = new ExampleEntities(); return(View(db.Product)); }