textbox content to a file

Hi Experts,
  i am trying to write program in C# . A simple webpage that has textbox and submit button. So the user types in text box it can be saved in a file(As a text file) in c:\\ drive on server.

I would appreciate your help with the code. Thanks,

sudhir
sudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudAuthor Commented:
Hi experts,
 Here is my code but giving error.


Filename Webform1.apsx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="WebForm1.aspx.cs" Inherits="TextUpload.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Height="243px" Width="351px"></asp:TextBox>
        <asp:Button ID="Btnupload" runat="server" Height="42px" Text="Submit" Width="127px" /></div>
    </form>
</body>
</html>


filename: Webform1.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace TextUpload
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        private void Button1_click(object sender, System.EventArgs e) {
           StreamWriter writer = new  StreamWriter("C:\\temp.txt");
writer.writeline(TextBox1.Text);
writer.close();

        }
    }
}


Thanks,

Sudhir
YurichCommented:
StreamWriter writer = new  StreamWriter("C:\\temp.txt");
writer.WriteLine( TextBox1.Text );
writer.Close();

WriteLine should be with W and L in capitals as well as C in Close...
Is that it?? Or you have some other errors - please let us see 'em and we may help with but other than above, your code should work just fine.

Regards,
Yurich

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudAuthor Commented:
hi Yurich ,
  i am getting the error:

Error:

The type or namespace name 'StreamWriter' could not be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\ssingh\My Documents\Visual Studio 2005\WebSites\WebSite5\WebForm1.aspx.cs   line 19      

Error      2      The type or namespace name 'StreamWriter' could not be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\ssingh\My Documents\Visual Studio 2005\WebSites\WebSite5\WebForm1.aspx.cs         line 1919

Thanks,

sudhir      
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

sudAuthor Commented:
hi,
  i did not use using System.IO;
I used it now so the error is gone, but i can not see the text file in C:\

Thanks,

sudhir
YurichCommented:
that's right, you have to use System.IO.

So  you don't have any errors any more? this three lines, if you fix those letters i mentioned, is just fine, i checked it. You can try to debug it and see what's happening for every line of your code.
sudAuthor Commented:
Hi Yurich,
 I am using ASP 2, and i am able to run the program after some modificaiton,
Here is the correct code for uploading text to a file, that is working on my system.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace TextUpload
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btnupload.Click += new System.EventHandler(this.Btnupload_click);
            this.Load += new System.EventHandler(this.Page_Load);


        }
        #endregion
        private void Btnupload_click(object sender, System.EventArgs e) {
            StreamWriter writer = new StreamWriter("C:\\temp.txt");
            writer.WriteLine(TextBox1.Text);
            writer.Close();
                   
        }
    }
}



Any idea why i need to initialize it, but you can run without initialization ?Thanks,

sudhir
YurichCommented:
hmm, what do you mean "initialize it"?? what initialize?

All coding you have to do is:

1) add your controls in the design time (a text-box and a button)

2) double-click your button

3) insert these three lines in handler for the button double-click:

            StreamWriter writer = new StreamWriter("C:\\temp.txt");
            writer.WriteLine(TextBox1.Text);
            writer.Close();

4) add
using System.IO;

if you question is about System.IO - you should add it if you want to use your StreamReader, alternatively, you can just put the following:

            System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\temp.txt");
            writer.WriteLine(TextBox1.Text);
            writer.Close();

and in this case you don't have to add using.

regards,
yurich



sudAuthor Commented:
Hi Yuirich,
     The program did not worked unless i added these lines of code to it,
 #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

              private void InitializeComponent()
        {
            this.Btnupload.Click += new System.EventHandler(this.Btnupload_click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

Any explanation ???
Thanks,
Sudhir
YurichCommented:
Oh, yes of course.

1) The question is not that why you had to add it but why it wasn't there.

if you create any new web-form application, this block:

#region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

              private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
#endregion

will be added automatically by .Net designer as it stated in the first line. You might not notice it since it's inside the region and if you press - next to the region word, it will fold and all you see, will be:

+ Web Form Designer generated code

I would suggest that it might have got somehow accidently deleted and you had to add it.

2) This line:

this.Btnupload.Click += new System.EventHandler(this.Btnupload_click);

is added automatically to InitializeComponent function as soon as you double click your button in your design mode assign a handler to the Click event and it will create an event handler at the end of you class.

Regards,
Yurich
sudAuthor Commented:
Hi Yurich,
   Thanks for your comment.
  I am using visual web developer 2005 express edition beta, and it is somehow not adding that piece of code.

But anyway now i know that i need to add so i will add it manually. Thanks again,

sudhir
sudAuthor Commented:
Hi Yurich,

Fixed that , changed texbox properties to multiline.

Is it possible to validate first line of textbox="pharmgkbnumber      date      techid      sampleid      samplesource      -288      note      fail_to_amp"

Thanks,

Sudhir      

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.