I need a functioning login code for a windows form. my code that I made has some problems. I posted my code to prove I made Effort. thanks.

--------

Username = 123-incorrect
Password = 123-incorrect
Login Failed.

when
Username = onat12-correct
Password = sambuca888-correct
Login Accepted.

now when,
Username = onat12-correct
Password = 123-incorrect
Login Failed.

now when,
Username = 123-incorrect
Password = 123-incorrect
Will not validate
Verification failed

--------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
 
 
namespace Form_Login1
{
public partial class frm_login : Form
{
public frm_login()
{
InitializeComponent();
 
}
int usercode;
System.Data.OleDb.OleDbConnection con;
private void frm_login_Load(object sender, EventArgs e)
{//verify database is running
try
{
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbSys.mdb;User Id=admin;Password=;";
con.Open();
 
 
MessageBox.Show("Connection Verified, Cleared to login", "O-Connection Has Been Verified-O",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
con.Close();
con.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "X-Database Error-X",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
this.Close();
 
}
 
//end database verification
}
 
private void btn_try_Click(object sender, EventArgs e)
{//login
using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbSys.mdb;User Id=admin;Password=;"))
{
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM users WHERE UserName=@UserName AND PassWord_=@PassWord_", cn))
{
cmd.Parameters.Add("@UserName", OleDbType.WChar, 50, "UserName").Value = this.tbx_user.Text;
 
cmd.Parameters.Add("@Password", OleDbType.WChar, 50, "PassWord").Value = this.tbx_pwd.Text;
 
if (cn.State == ConnectionState.Closed)
{
cn.Open();
 
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
if (rdr.HasRows)
{
rdr.Read();
 
if ((rdr.GetString(1) == this.tbx_user.Text) && (rdr.GetString(2) == this.tbx_pwd.Text))
{
MessageBox.Show("Login Succesful");
con.Close();
con.Dispose();
usercode =
MainPage f = new MainPage(usercode);
f.Show();
this.Close();
;
 
 
}
else
{
 
MessageBox.Show("Verification Failed");
tbx_pwd.Text = "";
tbx_user.Text = "";
}
}
 
}
}
}
}
 
}
 
private void btn_abrt_Click(object sender, EventArgs e)
{//close
con.Close();
con.Dispose();
this.Close();
}

Recommended Answers

All 3 Replies

How in the world can you destroy all formatting when you're obviously using Visual Studio to write the code in the first place?

protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtUserName.Text != "" && txtPassword.Text != "")
            {
                string connection= con.GetConnection();
                string lname = txtUserName.Text;

                MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
                byte[] hashedDataBytes;
                UTF8Encoding encoder = new UTF8Encoding();
                hashedDataBytes =md5Hasher.ComputeHash(encoder.GetBytes(txtPassword.Text.Trim()));

                //string pname = txtPassword.Text;
                
                OleDbConnection conn = new OleDbConnection(connection);
                string qry = "select LogName,LogPassword from LoginTable where LogName='" + lname + "' and LogPassword='" + hashedDataBytes + "'";
                OleDbDataAdapter oda = new OleDbDataAdapter(qry, conn);
                DataSet ds = new DataSet();
                
                oda.Fill(ds, "StudentTable");
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    Session["UserName"] = lname;
                    Response.Redirect("MainMenu.aspx");
                }
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('UserName & Password is Not Blank.')</script>");
            }
        }
        catch (Exception er)
        {
            Response.Write("Error" + er);
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.