Simultaneous entry ASP form error

This is a discussion on "Simultaneous entry ASP form error" within the Classic ASP section. This forum, and the thread "Simultaneous entry ASP form error are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Classic ASP

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old May 21st, 2007, 08:01
welshstew's Avatar
Site Admin

SuperMember
Join Date: May 2007
Location: inside the outside
Posts: 1,716
Blog Entries: 14
Thanks: 4
Thanked 33 Times in 31 Posts
Simultaneous entry ASP form error

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>
Last Blog Entry: Phorm approved for UK rollout (Sep 17th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 21st, 2007, 16:01
welshstew's Avatar
Site Admin

SuperMember
Join Date: May 2007
Location: inside the outside
Posts: 1,716
Blog Entries: 14
Thanks: 4
Thanked 33 Times in 31 Posts
Re: Simultaneous entry ASP form error

I've managed to close the connection to the file, which should help, and added a try catch block arounf the whole thing so that it just reloads the page on error

Anyone got any other ideas on how to improve this so that it stops freezing up on me?

Code: Select all
<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<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_0607\\experience_"+ 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: Phorm approved for UK rollout (Sep 17th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
asp, form

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form error messages help psycho wolvesbane JavaScript Forum 0 Mar 21st, 2008 14:50
PHP Mail Submit Form Error longstand PHP Forum 6 Nov 11th, 2007 16:02
PHP Form returns error : mail expects most 5 parameters? shifty PHP Forum 5 Sep 30th, 2007 21:07
file lock on entry form welshstew ASP.NET Forum 1 Jun 7th, 2007 08:22
Javascript Form error drappendix JavaScript Forum 4 Jan 5th, 2007 00:53


All times are GMT. The time now is 12:16.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42