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 6611825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:01:18+00:00 2026-05-25T20:01:18+00:00

SqlConnection connection = new SqlConnection(@Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true); SqlCommand Command = connection.CreateCommand(); SqlDataReader SQLRD;

  • 0
SqlConnection connection = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true");
SqlCommand Command = connection.CreateCommand();

SqlDataReader SQLRD;

Command.CommandText = "Select * from Attendance";

connection.Open();
SQLRD = Command.ExecuteReader();

string data = "";

while (SQLRD.Read())
{
    data += SQLRD[0].ToString()+  "\n";
    data += SQLRD[1].ToString() + "\n";
    data += SQLRD[2].ToString() + "\n";
    data += SQLRD[3].ToString() + "\n";
    data += SQLRD[4].ToString() + "\n";
    data += SQLRD[5].ToString() + "\n";
    data += SQLRD[6].ToString() + "\n";
    data += SQLRD[7].ToString() + "\n";
}

SQLRD.Close();
connection.Close();

string filename = @"C:\download.csv";//specified location
FileStream fs = new FileStream(filename,FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(data);
sw.Flush();
sw.Close();
fs.Close();

This is what i have done so far. currently upon clicking on download all information appear in 1 single column and is not separated into rows. I need to know how can i arrange them into rows. Also can i also know how am i to display a dialog box when the user clicks on download. Currently the file is just stored in the specified location?

  • 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-25T20:01:19+00:00Added an answer on May 25, 2026 at 8:01 pm

    When exporting to .csv format, you want to put commas (,) between the columns, and line returns (\n) between the rows. Currently, you are putting a line return between every single column. Try something like this:

    while (SQLRD.Read())
    {
         data += SQLRD[0].ToString() + ",";
                       //              ^^^ note the change from '\n' to ','
         data += SQLRD[1].ToString() + ",";
         data += SQLRD[2].ToString() + ",";
         ...
         data += SQLRD[7].ToString(); // final column doesn't need a ','
         data += "\n"; // final line separator for the entire row
    }
    

    Best regards,

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

Sidebar

Related Questions

I had someting like this in my code (.Net 2.0, MS SQL) SqlConnection connection
Does Java Connection.close rollback into a finally block?. I know .Net SqlConnection.close does it.
In C++, how can I establish an SQL connection to store data in an
Assume the following code: using (SqlConnection conn = new SqlConnection(connectionString)) { ... using (SqlCommand
It's easy to bind a data source to something like a gridview or repeater,
My code for sql connection using linq is: var query1 = from u in
Given an SQLConnection object how can you get a schema for a single table?
If I wrap a SQLConnection in a Using, should I close it or does
Application 1 - Opens a SqlConnection and a SqlTransaction against a SQLServer 2005 database
If you need to open a SqlConnection before issuing queries, can you simply handle

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.