WoLv3rine
WoLv3rine

Reputation: 107

Exporting contents of a TextBox to a Text File Visual Studio C#

I am trying to export the contents of a textbox to a txt file at the press of a button "Export". I am using web application forms in Visual Studio in C#.

I have got the txt file creation part working at the press of a button. But I am unable to export the data from the textbox into the text file.

The textbox's contents are referencing a DataGrid so how can I link the program in C# so that at the press of the button "Export" it transfers the contents fo the textbox into the text file?

So the code below is what I have to create a text file. What would I need to add to this, to do the above?

    private void Export_Click(object sender, EventArgs e)
    {
         //DataRowView drv = ((DataRowView)ordersBindingSource.Current);
         //DataRow dr = drv.Row;        

        string path = @"G:\bin\Debug\Test.txt";
        if (!File.Exists(path))
        {
            // Create a file to write to. 
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hi," + System.Environment.NewLine);
                sw.WriteLine("Order ID: ");
            }
        }
    }

Please Help!! I am working on a project and stuck at this level of exporting information.

Upvotes: 0

Views: 3375

Answers (1)

Fabske
Fabske

Reputation: 2126

Put your textbox in a form
Then set the Export button to be a submit button (also in the form)

On the server, when the post is done, you'll be able to access to the textbox content (this.Forms if I'm not wrong, it's a long time I didn't use asp.net)

Once you have the textbox content, you just need to use

System.IO.File.WriteAllText(@"G:\bin\Debug\Test.txt", textBoxContent)

Be carefull: Asp.net doesn't have the permissions to write anywhere on the disk, you may need to give permission on a specific directory

Upvotes: 2

Related Questions