Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:45:57+00:00 2026-05-29T17:45:57+00:00

I having issues exporting a GridView to Excel for some reason. I have two

  • 0

I having issues exporting a GridView to Excel for some reason. I have two buttons, one is the Search which handles the search once the user has provided the required information. The other button is the Export which basically handles the export of the gridview to excel.

My problem is that when an user clicks on the search button and then they want to export the data to excel they need to click on the Export button. Everything is good until this point, when the excel file is view, no data is exported. Here is my code for both buttons:

Any help will be appreciate it, thanks.

protected void search(object sender, EventArgs e)
{
    odbc.Open();
    ds = new DataSet();
    cmd = new OdbcCommand("SELECT XHLBCD AS LOCATION, XHLCST AS STATUS, XHEXUN AS EXCESS, XHSHUN AS SHORT, XHCNTD AS DATE_COUNTED FROM " +
                            "WM242BASD.XHCTRL00 WHERE XHCNTD BETWEEN '" + fromdate.Text + "' AND '" + todate.Text + "'", odbc);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = odbc;
    oda = new OdbcDataAdapter(cmd);
    oda.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    odbc.Close();

}

protected void export_OnClick(object sender, EventArgs e)
{

    // Let's hide all unwanted stuffing
    GridView1.AllowPaging = false;
    GridView1.AllowSorting = false;

    // Let's bind data to GridView
    BindGrid();

    //Change the color back to white
    GridView1.HeaderRow.Style.Add("background-color", "#ffffff");

    //Apply color to the header
    GridView1.HeaderRow.Cells[0].Style.Add("background-color", "#e0e0e0");
    GridView1.HeaderRow.Cells[1].Style.Add("background-color", "#e0e0e0");
    GridView1.HeaderRow.Cells[2].Style.Add("background-color", "#e0e0e0");
    GridView1.HeaderRow.Cells[3].Style.Add("background-color", "#e0e0e0");
    GridView1.HeaderRow.Cells[4].Style.Add("background-color", "#e0e0e0");

    // Let's output the GridView
    Response.Clear();
    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("content-disposition", "attachment;filename=" + reportid + ".xls");

    StringWriter swriter = new StringWriter();
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

    GridView1.RenderControl(hwriter);


    Response.Write(swriter.ToString());
    Response.End();

}

private void BindGrid()
{
    GridView1.DataBind();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:45:57+00:00Added an answer on May 29, 2026 at 5:45 pm

    You need to give the grid a datasource in both instances, you get no data from your export because the gridview hasn’t got a datasource at that point.

    Move the code from the search method into your databaind method and call the method from both event handlers.

    Also use SQL parameters you code is wide open for SQL injection attacks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im still having issues using HttpWebRequest . For some reason sometimes in my app
We are having some major performance issues with SELECT queries out one of our
Im having issues getting this to work, maybe its not even possible? I have
I am having an issue is that while exporting a report to excel sheet,
I'm having some issues wrapping my head around the concept of classloading, I've been
Currently, working with RC0 Denali, having some issues I am attempting to review data
Having issues getting django custom commands to work. From django documetation , have placed
I am having some issues with some Linq again where i am trying to
I'm kinda having some issues with linking my assembly. I use NASM for assembly
Having issues referencing $(this) from within a the nested ajax 'success' function... I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.