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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:17:17+00:00 2026-06-11T15:17:17+00:00

I am retrieving data from the SQL database and saving it ‘.txt’ as a

  • 0

I am retrieving data from the SQL database and saving it ‘.txt’ as a comma delimited file using a Web Application as developing environment.
The only problem I have is that at the start of the first column it add some garbage characters.

Ex. ¼¤Date, Time

Tried to use Encoding.ASCII and new UTF8Encoding(false) but solved the bug.

But the rest of the data is perfect. The code below is the one used to save the data.

        System.IO.StringWriter sWriter;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sWriter = new System.IO.StringWriter(sb);
        string Str;

        for (int k = 0; k < (grdGridView.Columns.Count); k++)
        {
            if (k == (grdGridView.Columns.Count) - 1)
                sWriter.Write(grdGridView.HeaderRow.Cells[k].Text);
            else
                sWriter.Write(grdGridView.HeaderRow.Cells[k].Text + ","); 
        }

        sWriter.WriteLine("");

        for (int i = 0; i < (grdGridView.Rows.Count); i++)
        {

            for (int j = 0; j < (grdGridView.Columns.Count); j++)
            {

                Str = (grdGridView.Rows[i].Cells[j].Text.ToString());

                if (Str == "&nbsp;")
                    Str = "";

                if (j != (grdGridView.Columns.Count) - 1)
                { Str = (Str + ","); }

                sWriter.Write(Str);
            }
            sWriter.WriteLine();
        }
        sWriter.Close();

        // Download File Directly to Web Server
        //string HtmlInfo = sWriter.ToString().Trim();
        string HtmlInfo = sWriter.ToString();
        string DocFileName = fileName1 + ".txt";
        string FilePathName = Server.MapPath("~/Download");
        FilePathName = FilePathName + "\\" + DocFileName;
        File.Delete(FilePathName);
        FileStream Fs = new FileStream(FilePathName, FileMode.Create);
        BinaryWriter BWriter = new BinaryWriter(Fs, Encoding.GetEncoding("UTF-8"));
        BWriter.Write(HtmlInfo);
        BWriter.Close();
        Fs.Close();
  • 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-06-11T15:17:19+00:00Added an answer on June 11, 2026 at 3:17 pm
     string HtmlInfo = sWriter.ToString();
     ....
     BinaryWriter BWriter = ...
     BWriter.Write(HtmlInfo);
    

    Using a BinaryWriter makes little sense here. The characters you see are the Length-prefix that is prependeded to a string by a BinaryWriter.

    Use a TextWriter instead, or get the contents of sWriter as a byte[].

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

Sidebar

Related Questions

I have worked on retrieving the data from SQL database using Datareader and populate
I took an video from you tube to Retrieving Data from SQL Server using
I am retrieving data from sql database. I want to split the records and
I am retrieving data from my SQL database... data exactly as it is in
I'm retrieving data from a MySQL table and displaying it on a webpage using
I am retrieving data from an Oracle database and binding the same to a
Traditionally when using a DbCommand when retrieving data from a sproc, something like the
For some reason I'm having a problem retrieving data from my database. It leaves
I have developed a VB.Net code for retrieving data from excel file .I load
Currently, I'm retrieving some data from a MySQL database table, but the problem is

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.