Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to retrive username and password from access to C#.net i'm using the following code but it is not working,
C#
public void login()
      {
 string file_name;
            file_name = Application.StartupPath + "\\" + "username.txt";
            System.IO.StreamWriter objwriter;
            objwriter = new System.IO.StreamWriter(file_name);
            objwriter.WriteLine(textBox1.Text);
            objwriter.Close();
            string nam;
            WeightBridge.Class1 obj = new WeightBridge.Class1();
            nam = obj.get();
            OleDbConnection con = new OleDbConnection(nam);
            //OleDbConnection con = new OleDbConnection("D://weightbridge.mdb");
            con.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT UserName ,pwd FROM User WHERE UserName='" + textBox1.Text + "' AND Password='" + textBox2.Text + "'", con);
            OleDbDataReader dr = cmd.ExecuteReader();
            string userText = textBox1.Text;
            string passText = textBox2.Text;
            while (dr.Read())
            {
                if ((dr["UserName"].ToString() == userText) && (dr["Password"].ToString() == passText))
                {
                    this.Hide();
                    MasterPage mp = new MasterPage();
                    mp.Show();
                }
                else
                {
                    MessageBox.Show("Invalid username");

                }
            }
            dr.Close();
            con.Close();
}
Posted
Updated 2-Jan-13 20:27pm
v2
Comments
Prasad_Kulkarni 3-Jan-13 2:37am    
You getting any error or what?
Balamurugan1989 3-Jan-13 2:40am    
I'm getting tis error:
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

 
Share this answer
 
v2
Comments
Balamurugan1989 3-Jan-13 6:13am    
I have done it but its not working...
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" && textBox2.Text == "")
{
MessageBox.Show("Enter the User name and Password");
// label6.Text ="Admin Name and Password Required.";
textBox1.Focus();
}
else
{
string nam;
WeightBridge.Class1 obj = new WeightBridge.Class1();
nam = obj.get();
nam="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+Application.StartupPath+"//weightbridge.accdb;Persist Security Info=False;";
OleDbConnection con = new OleDbConnection(nam);
con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT UserName ,pwd FROM [User] WHERE UserName='" + textBox1.Text + "' AND pwd='" + textBox2.Text + "'", con);
OleDbDataReader dr = cmd.ExecuteReader();
string userText = textBox1.Text;
string passText = textBox2.Text;
if (dr.Read())
{

this.Hide();
MasterPage mp = new MasterPage();
mp.Show();


//login();
dr.Close();
con.Close();
}
else
{
MessageBox.Show("UserName/Password Invalid");
}
 
Share this answer
 
Comments
fjdiewornncalwe 21-Feb-13 16:44pm    
My 1: SQL Injection Nightmare.

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