Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to end a request if there are no records to display

How to end a request if there are no records to display

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorialquestion
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meeram395
    wrote on last edited by
    #1

    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.

    N S 2 Replies Last reply
    0
    • M meeram395

      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.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Why are you creating a GridView if you want to create an Excel file?


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • M meeram395

        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.

        S Offline
        S Offline
        surender m
        wrote on last edited by
        #3

        Try response.ContentType = "application/octet-stream"; Hope this helps you.. No Defeat Is Final Until You Stop Trying!.......

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups