I'm having some trouble with an
asp form that hopefully you can help me with. It's just a simple form that writes to a text file. However. when I am doing my stress testing it comes up with an
asp error page. unfortunatly, I am unable to debug this, as it only appears within the live secure (
https://) environment.
I beliee that what is ahppening is that when the text file is opened by an entry it is locked, and if another individual tries to enter at exactly the same time, this is when the error is occuring.
Is there anyway of changing the code below so that I do not come across this issue? maybe a wait 5 seconds and then resubmit?
- Code: Select all
<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
void Page_Load(Object semder, EventArgs e)
{
lblError.Visible = false;
if (!Page.IsPostBack)
{
}
else
{
Page.Validate();
if (Page.IsValid)
{
// get the form values
string firstName = txtFirstName.Text.Trim();
string middleName = txtMiddleName.Text.Trim();
string lastName = txtLastName.Text.Trim();
string postcode = txtPostcode.Text.Trim();
string phone = txtPhone.Text.Trim();
//string optin = radFurtherInfo.SelectedValue;
string datenow = System.DateTime.Today.ToString("dd-MMM-yyyy");
// write 'em out to a text file
if (!WriteToTextFile(firstName, middleName, lastName, postcode, phone, datenow))
{
lblError.Visible = true;
}
// redirect to the site
Response.Redirect("entryform_success.html");
}
}
}
private bool WriteToTextFile(string firstName, string middleName, string lastName, string postcode, string phone, string datenow)
{
bool success = true;
try
{
// Change this to the location you want to save the data
// Be careful with permissions on this file, it shouldn't be browsable
// E.g. string filename = "d:\\webdata\\events\\"
string fileName = "c:\\comp_0607\\experience_"+ datenow +".txt";
StreamWriter sw = new StreamWriter(fileName, true);
sw.WriteLine(firstName.Replace(",", "") + "," + middleName.Replace(",", "") + "," + lastName.Replace(",", "")
+ "," + postcode.Replace(",", "") + "," + phone + ","+ datenow);
sw.Close();
}
catch(Exception ex)
{
lblError.Text = ex.Message;
success = false;
}
return success;
}
</script>