使用Facebook登录&与网站整合 [英] Using Facebook login & integrate with website

查看:58
本文介绍了使用Facebook登录&与网站整合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我差不多要完成这个了。我从aspsnippets获取代码。刚添加了一些我的代码来检查条件。有两个按钮一个登录facebook&获取正在工作的信息然后在第二个按钮上单击它检查我的数据库中是否存在userid(从fb获取)?如果没有,则将其插入db。但是在执行结束时我得到了这个例外



System.InvalidOperationException:连接已经打开。在MySql.Data.MySqlClient.ExceptionInterceptor.Throw(例外的例外)在MySql.Data.MySqlClient.MySqlConnection.Throw(异常前)在MySql.Data.MySqlClient.MySqlConnection.Open()在login.Button1_Click(对象发件人,EventArgs的)在C:\ Users \ Suraj \Desktop \fbLoginTest \login.aspx.vb:第44行



我尝试过:



Private Sub Button1_Click(发送者为对象,e为EventArgs)处理Button1.Click

如果不是String.IsNullOrEmpty( lblId.Text)然后

Dim query As String =选择ID,电子邮件来自ID = @id的用户

con.Open()

尺寸CMD作为新的MySqlCommand(查询,CON)

cmd.Parameters.AddWithValue( @ ID,lblId.Text)

尺寸博士担任了MySqlDataReader = CMD。 ExecuteReader()

如果dr.HasRows那么

'这里做登录代码

试试

Dim str As String =select * from users where ID ='+ lblId.Text +';

Dim cmd2 As New MySqlCommand(str,con)

con.Open()

Dim da As New MySqlDataAdapter(cmd2)



Response.Cookies(User_Type)。Value =users

Response.Cookies(chkusername)。Value = lblId.Text

Response.Redirect(Request.Url.AbsoluteUri)

Catch ex as Exception

Response.Write(ex)

con.Close()

结束尝试

Else

尝试

Dim str1 As String =INSERT INTO users(ID,DP,displayName)values('+ lblId.Text +','+ ProfileImage.ImageUrl .ToString +','+ lblName.Text +')

Dim str2 As MySqlDataReader

Dim adapter As New MySqlDataAdapter

Dim命令As New MySqlCommand

command.CommandText = str1

命令。 Connection = con

adapter.SelectCommand = command

con.Open()

str2 = command.ExecuteReader

con.Close()

Catch ex As Exception

Response.Write(ex)

结束尝试

结束如果

结束如果

con.Close()

End Sub

I am almost about to finish this. I have taken code from aspsnippets. Just added some of my code to check the conditions. There are two buttons One do login from facebook & fetch information which is working Then on second button click it checks whether userid(got from fb) is exist in my db or not? If not then Insert it in db. But at the end of execution I am getting this exception

System.InvalidOperationException: The connection is already open. at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception) at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) at MySql.Data.MySqlClient.MySqlConnection.Open() at login.Button1_Click(Object sender, EventArgs e) in C:\Users\Suraj\Desktop\fbLoginTest\login.aspx.vb:line 44

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
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.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)
con.Open()
Dim da As New MySqlDataAdapter(cmd2)

Response.Cookies("User_Type").Value = "users"
Response.Cookies("chkusername").Value = lblId.Text
Response.Redirect(Request.Url.AbsoluteUri)
Catch ex As Exception
Response.Write(ex)
con.Close()
End Try
Else
Try
Dim str1 As String = "INSERT INTO users (ID, DP, displayName) values('" + lblId.Text + "', '" + ProfileImage.ImageUrl.ToString + "', '" + lblName.Text + "')"
Dim str2 As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = str1
command.Connection = con
adapter.SelectCommand = command
con.Open()
str2 = command.ExecuteReader
con.Close()
Catch ex As Exception
Response.Write(ex)
End Try
End If
End If
con.Close()
End Sub

推荐答案

查看您的代码:

您打开相同的连接三次,只有在前几次出现错误时才关闭它。

而不是抓取代码碎片并试图将它们连接在一起,你需要sta rt思考它们,并根据你的需要调整它们。

在这种情况下,无论你从哪里获得碎片都不是那么好。使用带有MySqlConnections,MySqlCommands,MySqlDataAdapter等的使用块是一个非常好的主意,以确保它们在超出范围时自动关闭和处置。这样你就不会犯这样的错误。

Look at your code:
You open the same connection three times, and only close it if there is an error for the first couple of times.
Instead of grabbing code fragments and trying to bolt them together, you need to start thinking about them, and adapting them to fit your needs.
And in this case, wherever you got the fragments from isn't that good. It is a very good idea to use a Using block with MySqlConnections, MySqlCommands, MySqlDataAdapter, and so forth to ensure that they are Closed and Disposed automatically when they go out of scope. This removes the opportunity for you to make mistakes like this.
Using con As New MySqlConnection(strCon)
	con.Open()
        Using cmd As New MySqlCommand(query, con)
                ...
        End Using
        Using cmd2 As New MySqlCommand(query, con)
                ...
        End Using
        ...
End Using


这篇关于使用Facebook登录&与网站整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆