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

  • Home
  • SEARCH
  • 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 7065765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:59:46+00:00 2026-05-28T04:59:46+00:00

I am trying to execute a query using for loop. For each loop it

  • 0

I am trying to execute a query using for loop. For each loop it should download an excel file. The solution works perfectly, but only the first file is downloaded and the other two files are not downloaded. I have also attached the complete code below.

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Btn_Click(object sender, EventArgs e)
    {
        DataTable it = GetList();
        foreach(DataRow dr in it.Rows)
        {
            string a = dr[0].ToString();
            for (int i = 0; i < 3; i++) 
            {
                string inm = it.Rows[i][0].ToString();
                DataTable gt = GetData(inm);
                ExportToSpreadsheet(gt, "Samples");
            }
        }
    }

    public DataTable GetData(string i)
    {

        SqlCommand command = null;
        SqlConnection conn = null;
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        conn.Open();
        command = new SqlCommand("SELECT id, name, class FROM  StudentTable WHERE (id = " + i + ") ORDER BY name";
        DataTable dt = new DataTable();
        SqlDataAdapter ada = new SqlDataAdapter(command);
        ada.Fill(dt);
        return dt;
    }

    public static void ExportToSpreadsheet(DataTable table, string name)
    {
        HttpContext context = HttpContext.Current;

        context.Response.ClearContent();
        context.Response.ContentType = "text/vnd.ms-excel";
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".xls");

        string tab = "";
        foreach (DataColumn dc in table.Columns)
        {
            context.Response.Write(tab + dc.ColumnName);
            tab = "\t";
        }
        context.Response.Write("\n");
        context.Response.Write("\n");
        int i;
        foreach (DataRow dr in table.Rows)
        {
            tab = "";
            for (i = 0; i < table.Columns.Count; i++)
            {
                context.Response.Write(tab + dr[i].ToString());
                tab = "\t";
            }
            context.Response.Write("\n");
        }
        context.Response.End();
    }

I have seen posts similar to this. Some people have recommended that, zip files can be created on the server and download multiple excel files in “zip” folder. If it is possible, how can I implement it to the above solution?

  • 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-28T04:59:46+00:00Added an answer on May 28, 2026 at 4:59 am

    You’re calling response.end after creating the first file – which aborts the rest of the process.

    I don’t think you’ll be able to create 3 excel files for download using this method.

    As an alternative you could create 3 CSV files on disk, then use a zip library to zip them up.
    see zipping files

    Or you could use JET with an Excel connection string and use SQL Insert statements to push your data into an empty excel file. And use a different worksheet for each of your tables

    write to excel with JET (but this limits you to 32bit)

    Or you could use a third part control to write an excel file with the three tables as worksheets

    Infragistics excel control

    But if I were you – I’d just present the user with 3 different links they could click on.

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

Sidebar

Related Questions

I'm trying to execute a query that currently works in phpMyAdmin but it does
I'm trying to execute a query but I get an error: Unknown table alias
I am trying to execute this SQL query prior to restoring a .BAK file
I'm trying to execute a asp.net webservice using jquery. When I pass only one
I'm trying to execute the following query but I receive a runtime error stating
I'm trying to execute a query in PostgreSQL using the following code. It's written
I am trying to execute a query like show tables. But I don't know
I am trying to execute the following SQL query using MS SQL Server Management
I'm using the MySql.Data dll from MySql and trying to execute a query to
I'm trying to execute a query using C#. The problem is that, despite the

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.