Tools: MVC 3, Linq to Sql, Asp.net, C#.net Status: Almost newbie to MVC and Linq to sql
Background: Have created a webgrid which shows data from database (sql server 2012).
Problem: trying to export that data to Excel sheet, it's done successfully, Excel shows all data but before all excel throws error ?

and after clicking yes it displays data
Controller:
namespace EmployeeAttendance_app.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Precise Technology Consultants";
var DataContext = new EmployeeAtdDataContext();
//var EmployeeAtd = DataContext.GetAttendance_Sp();
IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
return View(EmployeeAtd);
}
public ActionResult About()
{
return View();
}
public ActionResult ToExcel()
{
var DataContext = new EmployeeAtdDataContext();
IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
System.Web.UI.WebControls.GridView gv = new System.Web.UI.WebControls.GridView();
gv.DataSource = DataContext.GetAtdRecord_Sp(null).ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=AttendanceSheet.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("Index");
}
}
}
View:
@using EmployeeAttendance_app.Models
<div>
@{
var grid = new WebGrid(ViewData.Model, defaultSort: "EmplID");
}
@grid.GetHtml()
</div>
Now this code given below is an old one and am trying to use it here in my code, so how ?
public ActionResult ExportData()
{
GridView gv = new GridView();
gv.DataSource = db.Studentrecord.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("StudentDetails");
}