Haseeb Zahid
Haseeb Zahid

Reputation: 656

ASP classic code keeps reloading

I have an ASP.NET website that includes few aspx pages that run the classic ASP code. The below query is written in classic asp code and it is using the App_Code file (dbConn) to run the query.

<%
SQL1 = "INSERT INTO table (field1, field2, field3) VALUES ('" & value1 & "', '" & value2 &    "', '" & value3 & "')"
dbConn.ExecuteCommand(SQL1)
%>

The problem that I am facing is that the file having the query, suppose it as "query.aspx", keeps recalling the query and filling the database with same entries infinite times even though the page "query.aspx" is called only once in the other file as

<!-- #include file="inc/query.aspx" -->

My question is, why does the page keeps executing the query over and over again even though it has been included only once in the entire project? Is the page "query.aspx" called more than once even if it is not included more than once?

Upvotes: 0

Views: 60

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

The first thing to understand here is that *.aspx pages are ASP.Net, not classic ASP.

With that information in hand, we also need to understand some things about how ASP.Net handles pages. If you are using web forms, ASP.Net does a lot of work to hide the HTTP Request/Repsonse model from you. Instead, you are presented with a model that tries to make a web page seem like a Windows Form: you get a form instance from which you can add and remove controls.

Except that's not quite right. You don't really get a form instance. You only get a Form type. ASP.Net does not completely hide the request/response model. Each request to your page works with a different instance of your form than the last request, even within the same user and session. Each control event that you handle results in a new request, and every time this happens, the ASP.Net runtime has to rebuild your entire page instance from scratch.

What this means is that one user viewing one ASP.Net page will cause the code for that page to run several times as they interact with the page. If you have an <!-- #include directive on that page, the directive will be re-executed for every control event.

To fix this, first replace the Query.aspx file with a database class that supports sending query parameter data separately from the sql command string. Then put code like this inside your *.aspx pages to use that new class:

Sub Page_Load
    If Not IsPostBack
        ' Call the database code from here
    End If
End Sub

Upvotes: 1

Related Questions