Wednesday, 28 December 2011

File Handling in ASP.NET : Insert into a file

protected void InsertIntoFile()
    {
        if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" && TextBox4.Text != "")
        {
            FileStream fs = new FileStream(Server.MapPath("Temp/temp.txt"), FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(TextBox1.Text);
            sw.WriteLine(TextBox2.Text);
            sw.WriteLine(TextBox3.Text);
            sw.WriteLine(TextBox4.Text);
            sw.WriteLine();
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        else Response.Write("Please fill all the fields.");

        total = 0;
        FileStream fs1 = new FileStream(Server.MapPath("Temp/temp.txt"), FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs1);

        while (!sr.EndOfStream)
        {
            for (int i = 0; i < 5; i++)
            {
                sr.ReadLine();
            }
            total++;
        }

    }

No comments:

Post a Comment