Jump to content

Recommended Posts

Posted (edited)

Bom dia eu estou a desenvolver um login mas estou com um problema.

Eu ao introduzir o user e a pass ele abre-me o form que desejo abrir e fecha-o logo alguem me pode explicar o porque ?

O código é este


Imports MySql.Data.MySqlClient
Imports System.Net.Sockets
Public Class LoginForm1
Const AppName = "Marky's Livechat System"
Dim clientSocket As New System.Net.Sockets.TcpClient()
Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	UsernameTextBox.Focus()
End Sub
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
	If UsernameTextBox.Text = "" Or PasswordTextBox.Text = "" Then
		MessageBox.Show("Please enter the required information to login.", AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
	Else
		Dim mMySQLConnectionString As String = "Server=localhost;Database=inforbew;Uid=root;password ="
		'You will need to change the connection string above to yours
		Dim conn As MySqlConnection
		Dim dr As MySqlDataReader
		Dim cmd As New MySqlCommand
		conn = New MySqlConnection()
		conn.ConnectionString = mMySQLConnectionString
		Try
			conn.Open() 'Always a good idea to open the connection in a try/catch block
			'Before you were selecting the username and password..
			'I changed that to the Status field being that its the field we need.
			cmd.CommandText = "SELECT nivel FROM login WHERE PK_Username = ?UserName AND Password = ?Password"
			cmd.Parameters.Add(New MySqlParameter("?UserName", UsernameTextBox.Text))
			cmd.Parameters.Add(New MySqlParameter("?Password", PasswordTextBox.Text))
			cmd.Connection = conn
			dr = cmd.ExecuteReader
			If dr.HasRows() Then
				Home.Show()
				Me.Close()

				'I used a datareader to hold the result of the Select Statement
				'which will be the status field if the username AND password are CORRECT
				'If they UserName and Password are wrong, the datareader will not hold any rows.
				dr.Read()
				Select Case UCase(dr(0).ToString)
					Case Is = "0"

						Home.Show()
					Case Is = "1"
						ListaClientes.Show()
					Case Is = "2"

						ListaClientes.Show()
				End Select
			Else
				MsgBox("Either the User Name or Password are Incorrect. Please try again.", MsgBoxStyle.Information, AppName)
				PasswordTextBox.ResetText()
				UsernameTextBox.ResetText()
				UsernameTextBox.Focus()
			End If
		Catch myerror As MySqlException
			MessageBox.Show("Database Error: " & myerror.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
		Catch ex As Exception
			MessageBox.Show("Error: " & ex.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
		Finally
			'The Finally Section of a Try Catch block will always fire, even when it runs into an exception.
			'That makes it a good place to clean up
			cmd.Parameters.Clear() 'I always clear my parameters when im done with them just to be safe
			conn.Close()
			cmd.Dispose()
			conn.Dispose()
		End Try

	End If
End Sub
Edited by Caça
GeSHi
Posted

Porque estas a fazer isto

Home.Show()
Me.Close()

Estas a abrir o form Home e logo depois fechas o actual, como o actual deve ser o primeiro formulário a arrancar, a aplicação termina.

Pedro Martins

Não respondo a duvidas por PM

Posted

Não é que não tinha reparado 😄

Outra coisa que não consigo fazer neste meu login e que ele não distingue na password maiúsculas de minúsculas, entra de qualquer maneira, como faço para não entrar ?

Posted

Nunca trabalhei com MySQL, mas no SQL Server bastava na query mudar a COLLATE dos campos para uma case sensitive, não sei se no MySQL também funciona assim..

Pedro Martins

Não respondo a duvidas por PM

Posted

Em SQL Server seria assim

SELECT nivel FROM login WHERE PK_Username = @UserName COLLATE SQL_Latin1_General_CP1_CS_AS  AND Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS

Pedro Martins

Não respondo a duvidas por PM

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.