views:

69

answers:

1

I have a slightly modified AccountController that should write a row into a table upon login.

However, the page redirects before the DB action has completed and so nothing is inserted.

I've temporarily solved this by sticking in a Thread.Sleep, but I'm looking for an alternative that makes it appear seamless.

    If Not String.IsNullOrEmpty(returnUrl) Then
        Return Redirect(returnUrl)
    Else
        Dim db As New GlobalSecModelDataContext
        Dim logAdmin = New secAdminLog
        logAdmin.EmployeeNumber = userName
        logAdmin.What = "Logged in"
        logAdmin.DateLogged = DateTime.Now
        logAdmin.UserIP = Request.ServerVariables("REMOTE_ADDR")
        logAdmin.NetworkUser = Request.ServerVariables("REMOTE_USER")

        db.secAdminLogs.InsertOnSubmit(logAdmin)
        db.SubmitChanges()
        Session("LoggedInUser") = StrConv(userName, VbStrConv.ProperCase)
        Threading.Thread.Sleep(5000)
        Return Redirect("/Home")
    End If

Is there a better solution that would be quicker for the user using the system or is using Threading.Thread.Sleep(5000) the best way?

I'm finding Threading.Thread.Sleep(5000) isn't perfect as the DB transaction doesn't always complete in that time.

Any suggestions would be helpful. Thanks in advance.

+1  A: 

Try wrapping your InsertOnSubmit and SubmitChanges into a transaction, and commit before the redirect. I have a lot of very similar code and don't see this issue. SubmitChanges() by default runs synchronously and will not return until the SQL has executed.

cdonner