Login checked Via a Database

a02227

Member
Joined
Nov 30, 2004
Messages
15
Location
Ireland
Programming Experience
Beginner
I'm trying to compare a user name entered into a textbox and searching to see if it is entered into the database, can anyone tell me if i'm heading in the right direction or completely wrong. At the min its not working, so any help would be much appreciated.

Function Search()

Dim cmd As New OleDb.OleDbCommand
Try

Me.OleDbConnection1.Open()
cmd.Connection = OleDbConnection1
cmd.Parameters.Add("@Membership No", Me.txtUser.text)
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select * from Members Where (Membership No = @Membership No)"
reader = cmd.ExecuteReader
reader.Read()
reader.Read.CompareTo(txtUser)
Return True
Catch ex As Exception
Response.Redirect("IncorrectUser.aspx")
End Try


Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
If Search(True) Then
Response.Redirect("MembersPage.aspx")
End If
End Sub
 
When using oledb for your connection, you have to indicate using a parameter in the statement by putting an "?". Don't give the parameter a name in the statement.


cmd.Parameters.Add("@Membership No", Me.txtUser.text)
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select * from Members Where (Membership No = ?)"

And I take it you want to read through all the records so do:
Dim strHelp As String

While reader.Read
strHelp = reader.getValue(0) 'This is the first column
<code for comparing>
End While
 
Instead of using a reader, I would suggest using the ExecuteNonQuery Function of the OleDbCommand. The ExecuteNonQuery function will execute a SQL statement against the connection and return the number of rows affected. If you execute a SELECT statement which contains the userName and a positive number is returned then the userName is already in the database.
 
I use this code and work very well:

VB.NET:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
	Dim clave1 As String = Trim(Me.txtUsuario.Text)
	Dim clave2 As String = Trim(Me.txtClave.Text)
	Me.con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\caritas.mdb"
	dta = New OleDbDataAdapter("Select * from Usuarios where IDUser = '" & clave1 & "' AND Clave = '" & clave2 & "' AND Nivel = 1", con)
	dst = New DataSet
	dta.Fill(dst, "Usuarios")
	con.Close()
	If Me.dst.Tables("Usuarios").Rows.Count >= 1 Then
	   ....Code.....
	  ' User OK.....
	Else
	   ....Code..... 	 
	  ' Access Denied.....
	End If
  End Sub

I hope this help you...
 
Back
Top