Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

Here is what I have done up til now, I have two text boxes and two buttons, with prett standard names my approch is like this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccessLogin
{
    public partial class LogIn : Form
    {
        public LogIn()
        {
            InitializeComponent();
        }
        OleDbConnection connect = new OleDbConnection();

        private void btnOk_Click(object sender, EventArgs e)
        {
            
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
            string UserName = txtUserName.Text;
            string PassWord = txtPassWord.Text;
            connect.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT UserName, PassWord FROM TAdminPW WHERE"); 
        }

    }
}


So now I think I am makeing connection and I need the logic where I should see that the entered user name and pass word exists in the DB if so the I can have access to the main program.
Thanks in advance for your assistence
Posted

Try this:
C#
string connectionString= @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
string sql= "select count(*) from TAdminPW where UserName=@UserName and [PassWord]=@Password"
using(OleDbConnection con = new OleDbConnection(connectionString))
using(OleDbCommand cmd = new OleDbCommand(sql, con))
{
     con.Open();
     cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
     cmd.Parameters.AddWithValue("@Password", txtPassWord.Text);
     int result = (int)cmd.ExecuteScalar()
     if(result > 0)
          MessageBox.Show("Login Successful.");
     else
         MessageBox.Show("Login Failed!");
}
 
Share this answer
 
Hai
Solution 1 is work fine,if u want ur coding way try this

C#
   private void btnOk_Click(object sender, EventArgs e)
        {
            
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
DataTable dt1 = new DataTable();
string strcmd1 = "";
OleDbDataAdapter cmd1 = new OleDbDataAdapter;
            string UserName = txtUserName.Text;
            string PassWord = txtPassWord.Text;
            connect.Open();
           strcmd1  = ("SELECT UserName, PassWord FROM TAdminPW WHERE UserName='" & txtUserName.Text.Trim() & "' and Password='" & txtPassWord.Text.Trim() & "'");


cmd1 = new OleDbDataAdapter(new OleDbCommand(strcmd1));
cmd1.SelectCommand.Connection = connect;

cmd1.Fill(dt1)
if ((dt1.Rows.Count > 0)) {
	//redirect to main page
} else {
	//show worng username or password error msg
}
        }
 
    }
}



Regards
Aravind
 
Share this answer
 
Try something like,
C#
private void btnOk_Click(object sender, EventArgs e)
{
    connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\XX\Documents\Visual Studio 2013\Projects\Access\Database.accdb;Persist Security Info=False";
    string UserName = txtUserName.Text;
    string PassWord = txtPassWord.Text;
    connect.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM TAdminPW WHERE UserName='"+userName.Txt+"',  Password='" +passwordTxt.Text+ "'"); 
    int n = cmd.ExecuteNonQuery();
    
    if(n > 0)
        MessageBox.Show("Login Successful.");
    else
        MessageBox.Show("Login Failure.");
}

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900