2016-06-06 2 views
1

누구든지 내 코드에 문제가 있다고 말할 수 있습니까? 나는 vb.net에서 새로운 개발 코드이므로 일부 참조 코드를 온라인으로 가지고있다. &이 코드를 작성하려고했지만 오류가 발생했다. & 내가 잘못하고있는 곳을 알려주십시오. 제 코드를 수정하십시오.이 연결과 관련된 DataReader는 먼저 닫아야합니다.

오류 먼저이 연결과 연결된 열려있는 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 [email protected]" 
      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 
+0

두 개의 중첩 된 명령에 대해 동일한 연결을 사용하고 있습니다. 내가 알고있는 한, 두 개의 별도 연결이 필요합니다. – Steve

+0

동일한 연결 내에서 다른 SQL 명령 ('select * from users, INSERT INTO users ... ')을 실행하기 전에 DataReader를 닫아야합니다. –

+0

@ 스티브 정류장 답변을 게시 할 수 있습니다 – SUN

답변

0

아무도 나에게 해결책을 제시하지 않았으므로 내 자신을 해결하려고했습니다. 따라서 사람들은 데이터 판독기를 닫으려고 제안했으나 새로운 개발자가되어서 데이터 판독기를 닫아야하는 것이 나의 질문이었습니다. 그것의 명령이 실행되기 전에 나는

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 [email protected]" 
      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 

스택 커뮤니티는 이제 최악의 점점 .. 다른 절에 내 DataReader를 폐쇄. 중재자가 거만하다

관련 문제