Sub enter_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim conn As New SqlConnection("server=localhost;database=education;uid=sa;pwd=")
        Dim strsql1 As String
        strsql1 = "select * from login where userid='" & userid.text & "'"
        Dim cmd As New SqlCommand(strsql1, conn)
        conn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.Read() Then
            message.Text = "帐号已经注册过"
            conn.Close()
        Else
            Dim strsql2 As String
            strsql2 = "insert into login(userid,password,sex,birthday,email,telephone,name) values ('" & userid.Text & "','" & password.Text & "','" & sex.SelectedItem.Text & "','" & birthday.Text & "','" & email.Text & "','" & telephone.Text & "','" & name.Text & "')"
            Dim cmdr As New SqlCommand(strsql2, conn)
            'Try
            cmdr.ExecuteNonQuery()
            conn.Close()
            Response.Redirect("index.aspx")
            '    Catch ex As Exception
            '        message.Text = "没有注册成功"
            '    End Try
        End If
    End Sub

解决方案 »

  1.   

    If dr.Read() Then
                message.Text = "帐号已经注册过"
                dr.Close()
                conn.Close()
            Else
                dr.Close()
                Dim strsql2 As String
                strsql2 = "insert into login(userid,password,sex,birthday,email,telephone,name) values ('" & userid.Text & "','" & password.Text & "','" & sex.SelectedItem.Text & "','" & birthday.Text & "','" & email.Text & "','" & telephone.Text & "','" & name.Text & "')"
                Dim cmdr As New SqlCommand(strsql2, conn)
                'Try
                cmdr.ExecuteNonQuery()
                conn.Close()
                Response.Redirect("index.aspx")
                '    Catch ex As Exception
                '        message.Text = "没有注册成功"
                '    End Try
            End If
      

  2.   

    dr.Close()
    conn.Close()
    ps:你的代码设计有问题...书写格式也不正规。
    建议
    try
    {
    }catch(Execption e)
    {
    }
    finally
    {
      if(dr!=null)
      dr.Close();
      if(con.State==ConnectionState.Open)
        conn.Close();
      
    }
      

  3.   


     If dr.Read() Then
                message.Text = "帐号已经注册过"            
            Else
                Dim strsql2 As String
                strsql2 = "insert into login(userid,password,sex,birthday,email,telephone,name) values ('" & userid.Text & "','" & password.Text & "','" & sex.SelectedItem.Text & "','" & birthday.Text & "','" & email.Text & "','" & telephone.Text & "','" & name.Text & "')"
                Dim cmdr As New SqlCommand(strsql2, conn)
                'Try
                cmdr.ExecuteNonQuery()
                           Response.Redirect("index.aspx")
                '    Catch ex As Exception
                '        message.Text = "没有注册成功"
                '    End Try
            End If
      dr.close()
       conn.Close()