2

这是发生了什么,如果用户已登录 - 这是直接从 Page_Load 调用的

Protected Sub EnterNewTransInDb()
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("connstring").ConnectionString)
        Dim comm As New SqlCommand("INSERT INTO tblRegisterRedirect (RegID , UserID, EventID, TimeStamp) VALUES (@RegID, @UserID, @EventID , getdate()) ;", conn)

        Dim RegID As Guid
        RegID = Guid.NewGuid()
        Dim GIUDuserid As Guid
        GIUDuserid = New Guid(HttpContext.Current.Request.Cookies("UserID").Value.ToString())
        Dim GIUDevnetid As New Guid(HttpContext.Current.Request.QueryString("id").ToString())
        comm.Parameters.AddWithValue("@RegID", RegID)
        comm.Parameters.AddWithValue("@UserID", GIUDuserid)
        comm.Parameters.AddWithValue("@EventID", GIUDevnetid)

        Try
            conn.Open()
            Dim i As Integer = comm.ExecuteNonQuery()
            conn.Close()
        Catch ex As Exception
            Dim errors As String = ex.ToString()
        End Try
        Dim URL As String = Request.QueryString("url").ToString()
        Response.Redirect(URL + "?aid=854&rid=" + RegID.ToString())
    End Sub

这很好用,但是如果他们没有登录,那么他们输入他们的登录凭据 - 这发生在 Button_Click 事件中,在点击事件中我调用这个函数EnterNewTransInDb(),当我这次运行它时,登录后 - 相同的代码,它引发异常 - 对象引用为空 - 引用查询字符串

 Protected Sub btnLogin_Click(sender As Object, e As System.EventArgs) Handles btnLogin.Click
      'took out code SqlConnection onnection and SqlDataReader Code
        dbCon.Open()            

        'If Email and PW are found
        If dr.Read Then
            Dim appCookie As New HttpCookie("UserID")
            appCookie.Value = dr("GUID").ToString()
            appCookie.Expires = DateTime.Now.AddDays(30)
            HttpContext.Current.Response.Cookies.Add(appCookie)

            Dim appCookie1 As New HttpCookie("UserName")
            appCookie1.Value = dr("UserName").ToString
            appCookie1.Expires = DateTime.Now.AddDays(30)
            HttpContext.Current.Response.Cookies.Add(appCookie1)

            Dim appCookie2 As New HttpCookie("UserEmail")
            appCookie2.Value = txtEmail.Text.ToLower()
            appCookie2.Expires = DateTime.Now.AddDays(30)
            HttpContext.Current.Response.Cookies.Add(appCookie2)

            Dim appCookie3 As New HttpCookie("Lat")
            appCookie3.Value = dr("GeoLat").ToString()
            appCookie3.Expires = DateTime.Now.AddDays(30)
            HttpContext.Current.Response.Cookies.Add(appCookie3)

            Dim appCookie4 As New HttpCookie("Long")
            appCookie4.Value = dr("GeoLong").ToString()
            appCookie4.Expires = DateTime.Now.AddDays(30)
            HttpContext.Current.Response.Cookies.Add(appCookie4)

            Dim appCookie5 As New HttpCookie("City")
            appCookie5.Value = dr("City").ToString()
            appCookie5.Expires = DateTime.Now.AddDays(30)
            HttpContext.Current.Response.Cookies.Add(appCookie5)

            Dim appCookie6 As New HttpCookie("State")
            appCookie6.Value = dr("State").ToString
            appCookie6.Expires = DateTime.Now.AddDays(30)
            HttpContext.Current.Response.Cookies.Add(appCookie6)

            HttpContext.Current.Response.Cookies("EO_Login").Expires = Now.AddDays(30)
            HttpContext.Current.Response.Cookies("EO_Login")("EMail") = txtEmail.Text.ToLower()

            Dim sUserData As String = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies("UserID").Value) & "|" & HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies("UserName").Value) & "|" & HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies("UserEmail").Value)
            ' Dim sUserData As String = "dbcf586f-82ac-4aef-8cd0-0809d20c70db|scott selby|scottselby@live.com"
            Dim fat As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, _
                     dr("UserName").ToString, DateTime.Now, _
                     DateTime.Now.AddDays(6), True, sUserData, _
                     FormsAuthentication.FormsCookiePath)
            Dim encTicket As String = FormsAuthentication.Encrypt(fat)
            HttpContext.Current.Response.Cookies.Add(New HttpCookie(FormsAuthentication.FormsCookieName, encTicket))

            'If Email and Pw are not found
        Else
            dr.Close()
            dbCon.Close()

        End If

        'Always do this
        dr.Close()
        sSql = "UPDATE eo_Users SET LastLogin=GETUTCDATE() WHERE GUID=@GUID; "
        cmd = New SqlCommand(sSql, dbCon)
        cmd.Parameters.AddWithValue("@GUID", HttpContext.Current.Session("UserID"))
        cmd.ExecuteNonQuery()
        dbCon.Close()
        EnterNewTransInDb()
        'Dim URL As String = Request.QueryString("url").ToString()
        'Response.Redirect(URL + "?aid=854&rid=" + RegID.ToString())
    End Sub
4

3 回答 3

0

尝试在调用之前设置断点并确保为变量分配了值。

于 2012-10-09T02:10:15.283 回答
0

假设您只希望此代码在存在有效 QueryString 的情况下运行,您可以在方法的开头放置一个保护子句来简单地检查 QueryString 是否为空,然后如果在没有 QueryString 的情况下调用此页面,则执行一些其他操作。

于 2012-10-09T02:14:44.237 回答
0

您是否尝试过在代码中设置断点Dim URL As String = Request.QueryString("url").ToString()?也许您只需要首先评估“url”参数的查询字符串(如果存在);在将其转换为字符串之前。

于 2012-10-09T02:33:45.283 回答