与此Connection关联的DataReader必须先关闭

时间:2016-06-06 15:00:44

标签: asp.net vb.net

有谁能告诉我我的代码有什么问题?我是vb.net开发的新手,因此在线获取了一些参考代码。试图编写此代码但收到错误。请告诉我我在哪里做错了&纠正我的代码。

ERROR 已经有一个与此Connection关联的开放DataReader,必须先关闭它。

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)
                    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)
                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
                    command.CommandText = str1
                    command.Connection = con
                    adapter.SelectCommand = command
                    command.ExecuteNonQuery()
                Catch ex As Exception
                    Response.Write(ex)
                End Try

            End If
        End If
        con.Close()
    End Sub

1 个答案:

答案 0 :(得分:0)

所以没有人给我解决方案我试图从我自己解决它。因此人们建议关闭数据阅读器,但这是我的问题,我需要关闭数据阅读器,因为我是一个新的开发人员。在执行命令之前,我在else子句中关闭了我的datareader。

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)
                    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)
                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()
                Catch ex As Exception
                    Response.Write(ex)
                End Try

            End If
        End If
        con.Close()
    End Sub

Stack社区现在变得更糟。主持人是傲慢的