So I've managed to get my admin area to export for the user to download into an xml file format. However I've put in place some filtering options so the user logged in can narrow down the results viewable. What I'm trying to acheive is then after the user applies these filters to be able to then export only the filtered data.
Code Below:
protected void ExportData_Click(object sender, EventArgs e)
{
string consString = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
StringBuilder sb = new StringBuilder();
using (SqlConnection con = new SqlConnection(consString))
{
con.Open();
string sql = ("SELECT [ID], [HouseNumber], [PropAddress], [Town], [County], [PostCode] FROM Zoopla;");
SqlCommand cmd = new SqlCommand(sql, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataBind();
cmd.Dispose();
con.Close();
string filename = "DownloadTest.xml";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
Filters are set as follows:
Filter By Weeks:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="TortoiseDBZoopla" DataTextField="Weeks" DataValueField="Weeks">
</asp:DropDownList>
Filter By Status:
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="ZooplaProperties" DataTextField="PropStatus" DataValueField="PropStatus">
</asp:DropDownList>