HELP: User Log In From Connected in Access Database

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 12
Reputation: boss16thugz is an unknown quantity at this point 
Solved Threads: 0
boss16thugz boss16thugz is offline Offline
Newbie Poster

HELP: User Log In From Connected in Access Database

 
0
  #1
Jul 17th, 2007
Hi I am currently new in C#, I have finished exercising and studying how to code for console application. Then I plan to do a simple application. I started with a user log in. Where in the form will validate the username and password inputted by the user in an access database and if successful it will show MAIN screen.

Below is my simple code, please help me. I am really stuck with this one once I new how to manipulate the records it will be easier for me. I don't know what to code in my cmdLogin. I cannot access there the datareader. Please help me I really want to learn.

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

namespace Perfume_Biz
{
public partial class Login : Form
{

public Login()
{
InitializeComponent();
string strconn = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=C:\\Perfume.mdb";
string strcmd = "select User_Name,Password from Users";
OleDbConnection OleConn = new OleDbConnection(strconn);
OleDbCommand OleCmd = new OleDbCommand(strcmd, OleConn);
OleConn.Open();
OleDbDataReader OleRead = OleCmd.ExecuteReader();
}

private void cmdcancel_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void cmdclear_Click(object sender, EventArgs e)
{
txtpassword.Text = "";
txtusername.Text = "";
}

private void Login_Load(object sender, EventArgs e)
{

}

private void cmdlogin_Click(object sender, EventArgs e)
{

}



}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: HELP: User Log In From Connected in Access Database

 
0
  #2
Jul 17th, 2007
If you want to use variables in more than one method, you should make them member variables instead of local variables.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: HELP: User Log In From Connected in Access Database

 
0
  #3
Jul 17th, 2007
Try to read the table row by row and compare user name and password for each read.Please look at the code sample below...

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.  
  4. OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data" + "Source=myDB.mdb");
  5. OleDbCommand com = new OleDbCommand("SELECT User_Name,Password FROM Table1",con);
  6. OleDbDataReader reader;
  7. bool permit = false;
  8.  
  9. try
  10. {
  11. con.Open();
  12. reader = com.ExecuteReader();
  13.  
  14. while (reader.Read())
  15. {
  16. if (InputName == (reader["User_Name"].ToString()) && InputPass == reader["Password"].ToString())
  17. {
  18. permit = true;
  19. break;
  20. }
  21. }
  22. con.Close();
  23. }
  24.  
  25. catch (Exception ex)
  26. {
  27. MessageBox.Show(ex.Message);
  28. }
  29.  
  30. if (permit == true)
  31. MessageBox.Show("Access granted");
  32. else
  33. MessageBox.Show("Acces denied");

You can also use a query which retrieves the user with the password in a try catch block.If no user found , catch the exception and tell it user.Here is a sample code below:

  1. OleDbDataReader read;
  2.  
  3. try
  4. {
  5. cmd = new OleDbCommand("SELECT User_Name FROM Table1 WHERE Password =" + Convert.ToInt32(textBox1.Text),con);
  6.  
  7. if(con.State == ConnectionState.Closed)
  8. con.Open();
  9.  
  10. read = cmd.ExecuteReader();
  11. read.Read();//You should read only once because you can //retrieve only one record(i.e, User_Name is unique)
  12.  
  13. textBox2.Text = read["User_Name"].ToString();
  14.  
  15.  
  16. read.Close();
  17. con.Close();
  18.  
  19.  
  20. }
  21.  
  22. catch (Exception ex)
  23. {
  24. MessageBox.Show(ex.Message);
  25. }
Last edited by FoX_; Jul 17th, 2007 at 11:39 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 12
Reputation: boss16thugz is an unknown quantity at this point 
Solved Threads: 0
boss16thugz boss16thugz is offline Offline
Newbie Poster

Re: HELP: User Log In From Connected in Access Database

 
0
  #4
Jul 17th, 2007
Hi,

thanks to both of you...now I can move one studying further C#. Thanks a lot.

Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC