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!![]()




Reply With Quote