Nathan Taylor
Nathan Taylor

Reputation: 24606

ASP.NET - Server Connection Reset

I have a website that from time to time requires I do bulk updates on the Access database and to facilitate this I created a run-once-and-delete page that contains a big text box, a captcha, and a hard coded strong password. When I run this against a local copy of the database I have no problems, but when I run it on the server I get a "Connection with Server Reset" about 3 seconds after clicking submit. Unfortunately the hosting environment is out of my purview so I cannot look at any server-side logs when this happens. I haven't the faintest idea what could be causing this so I was hoping you guys could take a look.

This code is a little ugly just because I didn't really care to spend a lot of time on it (and some of it was written by another developer). Either way, I don't see any obvious functional issues.

protected void btnRunBatch_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        ArrayList queries = GetSqlStatementArray();
        string results = string.Empty;

        try
        {
            BatchSQLInsert(Application["DBPath"].ToString(), queries);

            if (queries.Count > 0)
            {
                results = "<b>Batch Operation Completed Successfully.</b><br /><br />";
                results += "The following queries were executed:";
                results += "<ul>";
                foreach (string query in queries)
                {
                    results += "<li>" + query + "</li>";
                }
                results += "</ul>";

                this.tbxBatchStatement.Text = string.Empty;
            }
            else
            {
                results = "<b>No queries to execute.</b>";
            }
        }
        catch (Exception ex)
        {
            results = "<b>Execution Errors Encountered:</b><br />";
            results += "<ul><li>" + ex.Message + "</li></ul>";
        }

        this.phResults.Controls.Add(new LiteralControl(results));
    }
}

private ArrayList GetSqlStatementArray()
{
    ArrayList queries = new ArrayList();
    string[] lines = this.tbxBatchStatement.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    string lineBuffer = string.Empty;

    foreach (string line in lines)
    {
        if (lineBuffer == string.Empty)
        {
            if ((line.ToUpper().StartsWith("SELECT") || line.ToUpper().StartsWith("INSERT") || line.ToUpper().StartsWith("UPDATE") || line.ToUpper().StartsWith("DELETE")))
            {
                if (line.EndsWith(";"))
                    queries.Add(line);
                else
                    lineBuffer = line;
            }
        }
        else
        {
            lineBuffer += " " + line;
            if (line.EndsWith(";"))
            {
                queries.Add(lineBuffer);
                lineBuffer = string.Empty;
            }
        }
    }
    return queries;
}

public static void BatchSQLInsert(string DBPath, System.Collections.ArrayList sqlArray)
{
    System.Data.OleDb.OleDbCommand cmd = null;
    System.Data.OleDb.OleDbConnection conn = null;
    System.Data.OleDb.OleDbTransaction myTrans = null;
    int intRecsReturned = 0;
    int i = 0;

    if (sqlArray.Count > 0)
    {
        cmd = new System.Data.OleDb.OleDbCommand();
        conn = GetConnection(DBPath);
        myTrans = conn.BeginTransaction();

        try
        {
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = conn;
            cmd.Transaction = myTrans;
            while (i < sqlArray.Count)
            {
                cmd.CommandText = (string)(sqlArray[i]);
                intRecsReturned = cmd.ExecuteNonQuery();
                i++;
            }
            myTrans.Commit();
            conn.Close();
        }
        catch (Exception eee)
        {
            myTrans.Rollback();

            throw new Exception("BatchSQLInsert() failed-" + eee.Message + "\r\nArray-" + sqlArray.ToString());
        }
        finally
        {
            if (conn != null)
                conn.Dispose();
            if (cmd != null)
                cmd.Dispose();
        }
    }
}

Upvotes: 0

Views: 1213

Answers (1)

BigBlondeViking
BigBlondeViking

Reputation: 3963

Sounds like the process is too long running, you might need to adjust it Time Out settings in IIS... :(

Which i know you can't, sorry not really helpful-

Upvotes: 1

Related Questions