Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello Friends,
in my web page i am using two textboxes to add record in a text file. but the problem is that every time when i insert new record,previous record in the text file automatically delete,and the text file contains only the current record.
i want previous record present in the text file and in every entry textfile should update with previous record and new record.
kindly help me fix this issue.
Thanks in advance

my code is
ASP.NET
<asp:TextBox ID="txt1" runat="server"></asp:TextBox><br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox><br />
<asp:Button ID="btn" runat="server" Text="save" OnClick="btn_Click" />


and my code behind is like this
C#
protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Click(object sender, EventArgs e)
        {
            TextWriter tw = new StreamWriter(Server.MapPath("date.txt"));
            tw.WriteLine(txt1.Text + txt2.Text + System.DateTime.Now);
            tw.Close();
        }
Posted

1 solution

Text files don't work like that: when you open them with a StreamWriter it discards all existing content: http://msdn.microsoft.com/en-us/library/fysy0a4b(v=vs.110).aspx[^]
Try this instead;
C#
string path = Server.MapPath("date.txt");
File.AppendAllText(path, txt1.Text + txt2.Text + System.DateTime.Now + "\n");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900