|
-
Jun 25th, 2006, 09:32 PM
#1
Thread Starter
Lively Member
[2005] Need help with login
I have a registration page wich is working fine, so I can add user. The problem is the login page, it alwais says that the login is invalid and I cant figure out what is wrong. I am using mysql.
Here is the login code:
Imports:
VB Code:
Imports System.Web.Security
Imports System.Configuration
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Data.OleDb
Function:
VB Code:
Partial Class login2
Inherits System.Web.UI.Page
Public storecons As String
Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
storecons = _
"SELECT COUNT(*) AS Num_of_User " & _
"FROM tblUser " & _
"WHERE U_Name = @UserName AND U_Password = @Password "
Dim MyConn As MySqlConnection = New MySqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
Dim MyCmd As New MySqlCommand(storecons, MyConn)
MyCmd.CommandType = Data.CommandType.Text
Dim objParam1, objParam2 As MySqlParameter
objParam1 = MyCmd.Parameters.Add("@UserName", MySqlDbType.VarChar)
objParam2 = MyCmd.Parameters.Add("@Password", MySqlDbType.VarChar)
objParam1.Direction = Data.ParameterDirection.Input
objParam2.Direction = Data.ParameterDirection.Input
objParam1.Value = txtUserName.Text
objParam2.Value = txtPassword.Text
Dim objReader As OleDbDataReader
' ||||| Try, catch block!
Try
' ||||| Check if Connection to DB is already open, if not, then open a connection
If MyConn.State = Data.ConnectionState.Closed Then
' ||||| DB not already Open...so open it
MyConn.Open()
End If
' ||||| Create OleDb Data Reader
objReader = MyCmd.ExecuteScalar(Data.CommandBehavior.CloseConnection)
' ||||| Close the Reader and the Connection Closes with it
While objReader.Read()
If CStr(objReader.GetValue(0)) <> "1" Then
lblMessage.Text = "Invalid Login!"
Else
objReader.Close() ' ||||| Close the Connections & Reader
Return True
End If
End While
Catch ex As Exception
lblMessage.Text = "Error Connecting to Database!"
End Try
End Function
Login button:
VB Code:
Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
If Page.IsValid Then ' ||||| Meaning the Control Validation was successful!
' ||||| Connect to Database for User Validation |||||
If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False) ' ||||| default.aspx Page!
Session("User") = txtUserName.Text
Else
' ||||| Credentials are Invalid
lblMessage.Text = "Invalid Login!"
End If
End If
End Sub
I hope someone can help me!
Thanks!
-
Jun 27th, 2006, 05:12 AM
#2
Addicted Member
Re: [2005] Need help with login
Ok Well first off your call to the class code can only return a trure or false value but in the class code you have lines where your returning a string. Try this:
Code:
Partial Class login2
Inherits System.Web.UI.Page
Public storecons As String
Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
storecons = _
"SELECT COUNT(*) AS Num_of_User " & _
"FROM tblUser " & _
"WHERE U_Name = @UserName AND U_Password = @Password "
Dim MyConn As MySqlConnection = New MySqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
Dim MyCmd As New MySqlCommand(storecons, MyConn)
MyCmd.CommandType = Data.CommandType.Text
Dim objParam1, objParam2 As MySqlParameter
objParam1 = MyCmd.Parameters.Add("@UserName", MySqlDbType.VarChar)
objParam2 = MyCmd.Parameters.Add("@Password", MySqlDbType.VarChar)
objParam1.Direction = Data.ParameterDirection.Input
objParam2.Direction = Data.ParameterDirection.Input
objParam1.Value = txtUserName.Text
objParam2.Value = txtPassword.Text
Dim objReader As OleDbDataReader
' ||||| Try, catch block!
Try
' ||||| Check if Connection to DB is already open, if not, then open a connection
If MyConn.State = Data.ConnectionState.Closed Then
' ||||| DB not already Open...so open it
MyConn.Open()
Else
msgbox("Could Not Open data base")
Return False
End If
' ||||| Create OleDb Data Reader
objReader = MyCmd.ExecuteScalar(Data.CommandBehavior.CloseConnection)
' ||||| Close the Reader and the Connection Closes with it
While objReader.Read()
If CStr(objReader.GetValue(0)).toString() <> "1" Then
Return False
Else
objReader.Close() ' ||||| Close the Connections & Reader
Return True
End If
End While
Catch ex As Exception
msgbox(ex.Message.ToString())
Return False
End Try
End Function
End Class
And on the login page:
Code:
Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
If Page.IsValid Then ' ||||| Meaning the Control Validation was successful!
' ||||| Connect to Database for User Validation |||||
If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) = True Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False) ' ||||| default.aspx Page!
Session("User") = txtUserName.Text
Else
' ||||| Credentials are Invalid
lblMessage.Text = "Invalid Login!"
End If
End If
End Sub
That should work but its hard to be sure with out been able to test it...should that fail to work try tracing the line: CStr(objReader.GetValue(0)).toString() to see what's happening their i.e.
'Add After Line While objReader.Read()
msgbox(CStr(objReader.GetValue(0)).toString())
I hope that helps a bit
Last edited by rabid lemming; Jun 27th, 2006 at 05:15 AM.

I will wait for death with a smile and a big stick
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|