
Jun 5th, 2007, 09:24
|
 |
Lead Administrator
|
|
Join Date: May 2007
Location: inside the outside
Posts: 1,388
Thanks: 1
Thanked 17 Times in 15 Posts
|
|
|
file lock on entry form
Is anyone available to help??
I have a asp form that writes to a text file, however, if i get more than one user entering at exactly the same time i get a file lock issue. below is my code, is there anyway to stop this from happening??
Thanks in advance. stew
- Code: Select all
<script runat="server">
void Page_Load(Object semder, EventArgs e)
{
try
{
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 microsite
Response.Redirect("entryform_success.html");
}
}
}
catch(Exception ex)
{
//just swallow the error
}
}
private bool WriteToTextFile(string firstName, string middleName, string lastName, string postcode, string phone, string datenow)
{
bool success = true;
StreamWriter sw = null;
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_entry_07\\entry_"+ datenow +".txt";
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;
}
finally
{
if (sw != null)
sw.Close();
}
return success;
}
</script>
Last Blog Entry: Web Standards Curriculum Launched (Jul 8th, 2008)
|