How to end a request if there are no records to display
-
I have a page which runs a dynamic sql query, gets the output and show the output in an excel file on the spot. Everything is working fine. Issue is, whenever, there is no records to show, even the process is completed, the browser seems to be still running. I can see the icon at the top is rounding with the word "Connecting....". I tried use Response.End and Response.Flush. But still the same scenario. I want to end the process and give a message saying that No records found. Even though message is given,it's not displaying in the label. Following is my code for export to excel:
private void ExporttoExcel(string xmlPath)
{HttpResponse response = HttpContext.Current.Response; // first let's clean up the response.object response.Clear(); response.Charset = ""; // set the response mime type for excel response.ContentType = "application/ms-excel"; DataSet xportdata = new DataSet(); xportdata.ReadXml(xmlPath); if (xportdata != null) { if (xportdata.Tables.Count > 0) { if (xportdata.Tables\[0\].Rows.Count > 0) { StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView gv = new GridView(); gv.DataSource = xportdata.Tables\[0\]; gv.DataBind(); gv.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } else { lblErrorMessage.Text = "No Records Found."; } } else { lblErrorMessage.Text = "No Records Found."; Response.Flush(); //Response.End(); } } }
Am I using something wrong here? Could anybody please help me on this. Thanks
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
I have a page which runs a dynamic sql query, gets the output and show the output in an excel file on the spot. Everything is working fine. Issue is, whenever, there is no records to show, even the process is completed, the browser seems to be still running. I can see the icon at the top is rounding with the word "Connecting....". I tried use Response.End and Response.Flush. But still the same scenario. I want to end the process and give a message saying that No records found. Even though message is given,it's not displaying in the label. Following is my code for export to excel:
private void ExporttoExcel(string xmlPath)
{HttpResponse response = HttpContext.Current.Response; // first let's clean up the response.object response.Clear(); response.Charset = ""; // set the response mime type for excel response.ContentType = "application/ms-excel"; DataSet xportdata = new DataSet(); xportdata.ReadXml(xmlPath); if (xportdata != null) { if (xportdata.Tables.Count > 0) { if (xportdata.Tables\[0\].Rows.Count > 0) { StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView gv = new GridView(); gv.DataSource = xportdata.Tables\[0\]; gv.DataBind(); gv.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } else { lblErrorMessage.Text = "No Records Found."; } } else { lblErrorMessage.Text = "No Records Found."; Response.Flush(); //Response.End(); } } }
Am I using something wrong here? Could anybody please help me on this. Thanks
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Why are you creating a GridView if you want to create an Excel file?
I know the language. I've read a book. - _Madmatt
-
I have a page which runs a dynamic sql query, gets the output and show the output in an excel file on the spot. Everything is working fine. Issue is, whenever, there is no records to show, even the process is completed, the browser seems to be still running. I can see the icon at the top is rounding with the word "Connecting....". I tried use Response.End and Response.Flush. But still the same scenario. I want to end the process and give a message saying that No records found. Even though message is given,it's not displaying in the label. Following is my code for export to excel:
private void ExporttoExcel(string xmlPath)
{HttpResponse response = HttpContext.Current.Response; // first let's clean up the response.object response.Clear(); response.Charset = ""; // set the response mime type for excel response.ContentType = "application/ms-excel"; DataSet xportdata = new DataSet(); xportdata.ReadXml(xmlPath); if (xportdata != null) { if (xportdata.Tables.Count > 0) { if (xportdata.Tables\[0\].Rows.Count > 0) { StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView gv = new GridView(); gv.DataSource = xportdata.Tables\[0\]; gv.DataBind(); gv.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } else { lblErrorMessage.Text = "No Records Found."; } } else { lblErrorMessage.Text = "No Records Found."; Response.Flush(); //Response.End(); } } }
Am I using something wrong here? Could anybody please help me on this. Thanks
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Try response.ContentType = "application/octet-stream"; Hope this helps you.. No Defeat Is Final Until You Stop Trying!.......