سؤال

Technology - Asp.Net 4.5, SqlServer 2012

I have a (Master Page Web Application) that seems to randomly lock up on functions that do sql queries, inserts, or updates. The queries are used to populate GridView's, ListView's, DataTable's for whatever page happens to be requested or postback.

If only one user is on then there is no lockups or errors. If multiple users are on than it randomly locks up with error. The error stacktrace points to functions with database interaction. The last line of stacktrace is "ProcessRequestMain". I can't seem to figure this one out yet. Has anyone else ran across this issue?

Error Message Details:

Object reference not set to an instance of an object.

Error StackTrace:

at MudderTrucks.Items.LoadItems()
at MudderTrucks.Items.Page_Load(Oject.sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
هل كانت مفيدة؟

المحلول 2

The issue: Connection variable in a module. I overlooked this.

Dim MyConnection As New SqlClient.SqlConnection( _
        ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)

Answer: Remove the module level variable and add it as a parameter to your function

Public Function GetData2(ByVal Sql As String, _
         MyConnection As SqlClient.SqlConnection) As DataTable

    '...code...'

End Function

Note: As a better alternative to using public functions in a module, I've created a PageBase class and inherited it on the pages requiring the functions. I added the connection variable at the top of class as it will expose it only to the page inheriting it. Or... you can create the connection in the page code behind and pass it to the function.

Page Code Behind

Public Class MyPage
    Inherits PageBase

End Class

PageBase Class

Public Class PageBase
    Inherits System.Web.UI.Page

    Public MyConnection As New SqlClient.SqlConnection( _
        ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)

    Public Function GetData2(ByVal Sql As String) As DataTable

        '...code...'

    End Function

End Class

Conclusion: I tried both of these methods and they fixed the posted issue.

نصائح أخرى

I would check if you have uncommitted transactions as that can cause table locks. Also make sure db connections are being closed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top