当值不是nullorempty

时间:2016-06-13 08:12:17

标签: asp.net vb.net

我试图为我的网站登录fb。所以如果用户已经存在,我会设置条件然后它会进行登录&如果没有,则执行插入用户数据。

在下面的代码中,如果string不是Null或Empty,那么我正在尝试执行代码,但是通过在我的语句中放置断点,我看到如果string有值,那么rest代码不会执行。可能是什么原因?我做错了吗?我的第一个代码块工作正常(从Facebook获取用户数据)。问题出在我正在检查条件的第二个区块。

Private Sub login_Load(sender As Object, e As EventArgs) Handles Me.Load
        FaceBookConnect.API_Key = "XXXXXXX"
        FaceBookConnect.API_Secret = "XXXXXXX"
        If Not IsPostBack Then
            If Request.QueryString("error") = "access_denied" Then
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('User has denied access.')", True)
                Return
            End If

            Dim code As String = Request.QueryString("code")
            If Not String.IsNullOrEmpty(code) Then
                Dim data As String = FaceBookConnect.Fetch(code, "me")
                Dim faceBookUser As FaceBookUser = New JavaScriptSerializer().Deserialize(Of FaceBookUser)(data)
                faceBookUser.PictureUrl = String.Format("https://graph.facebook.com/{0}/picture", faceBookUser.Id)
                pnlFaceBookUser.Visible = True
                lblId.Text = faceBookUser.Id
                lblUserName.Text = faceBookUser.UserName
                lblName.Text = faceBookUser.Name
                lblEmail.Text = faceBookUser.Email
                ProfileImage.ImageUrl = faceBookUser.PictureUrl
                btnLogin.Enabled = False
            End If
        End If

        If IsPostBack Then
            If Not String.IsNullOrEmpty(lblId.Text) Then
                Dim query As String = "Select ID, email From users where ID=@id"
                con.Open()
                Dim cmd As New MySqlCommand(query, con)
                cmd.CommandTimeout = 50000
                cmd.Parameters.AddWithValue("@id", lblId.Text)
                Dim dr As MySqlDataReader = cmd.ExecuteReader()
                If dr.HasRows Then
                    'Do Login Code here
                    Try
                        Dim str As String = "select * from users where ID='" + lblId.Text + "';"
                        Dim cmd2 As New MySqlCommand(str, con)
                        Dim da As New MySqlDataAdapter(cmd2)
                        Response.Cookies("User_Type").Value = "users"
                        Response.Cookies("chkusername").Value = lblId.Text
                        Response.Redirect(Request.Url.AbsoluteUri)
                        Response.Write("User Already Exist")
                    Catch ex As Exception
                        Response.Write(ex)
                    End Try
                Else
                    Try
                        Dim str1 As String = "INSERT INTO users (ID, DP, displayName) values('" + lblId.Text + "', '" + ProfileImage.ImageUrl.ToString + "', '" + lblName.Text + "')"
                        Dim adapter As New MySqlDataAdapter
                        Dim command As New MySqlCommand
                        dr.Close()
                        command.CommandText = str1
                        command.Connection = con
                        adapter.SelectCommand = command
                        command.ExecuteNonQuery()
                        Response.Write("User Added")
                    Catch ex As Exception
                        Response.Write(ex)
                    End Try
                End If
                con.Close()
            End If
        End If
    End Sub
End Class

0 个答案:

没有答案