| | |
HELP: User Log In From Connected in Access Database
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2007
Posts: 12
Reputation:
Solved Threads: 0
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)
{
}
}
}
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)
{
}
}
}
Try to read the table row by row and compare user name and password for each read.Please look at the code sample below...
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:
C# Syntax (Toggle Plain Text)
private void button1_Click(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data" + "Source=myDB.mdb"); OleDbCommand com = new OleDbCommand("SELECT User_Name,Password FROM Table1",con); OleDbDataReader reader; bool permit = false; try { con.Open(); reader = com.ExecuteReader(); while (reader.Read()) { if (InputName == (reader["User_Name"].ToString()) && InputPass == reader["Password"].ToString()) { permit = true; break; } } con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } if (permit == true) MessageBox.Show("Access granted"); else 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:
C# Syntax (Toggle Plain Text)
OleDbDataReader read; try { cmd = new OleDbCommand("SELECT User_Name FROM Table1 WHERE Password =" + Convert.ToInt32(textBox1.Text),con); if(con.State == ConnectionState.Closed) con.Open(); read = cmd.ExecuteReader(); read.Read();//You should read only once because you can //retrieve only one record(i.e, User_Name is unique) textBox2.Text = read["User_Name"].ToString(); read.Close(); con.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); }
Last edited by FoX_; Jul 17th, 2007 at 11:39 am.
![]() |
Similar Threads
- Writing to an Access Database (Visual Basic 4 / 5 / 6)
- Inserting a new Access Database Record (VB.NET)
- I hava trouble with Microsoft Access database, Pls help me. (Java)
- Saving information from .NET webcontol into access(database) (ASP.NET)
- Searching for a string in an access database (Visual Basic 4 / 5 / 6)
- Registration and Login scripts using VB and Oledbconnection to Access Database (ASP.NET)
- JDBC for Access Database (Java)
Other Threads in the C# Forum
- Previous Thread: Custom UserControls Throwing Events Up the Hierarchy
- Next Thread: Reality Check on picturebox control (And creating custom control)
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox concurrency control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ globalization httpwebrequest image index input install java label list listbox listener mandelbrot math microsoftc#visualexpress mouseclick mysql networking operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





