查询字符串抛出异常,因为它不是

时间:2012-10-09 01:29:39

标签: asp.net query-string

以下是发生的事情,如果用户已登录 - 这是直接从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()的点击事件中,当我这次运行时,登录后 - SAME CODE,它抛出一个异常 - 对象引用为null - 引用查询字符串

 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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

假设您只希望在有一个有效的QueryString时运行此代码,您可以在方法的开头放置一个guard子句来简单地检查QueryString是否为null,然后在没有调用此页面的情况下执行其他操作。查询字符串。

答案 2 :(得分:0)

你有没有尝试在'Dim URL As String = Request.QueryString(“url”)上添加一个断点。代码中的ToString()'行?也许你只需要首先评估'url'参数的查询字符串,如果存在的话;在将其转换为字符串之前。