login form authentication
hi,
i am developing a windows application and i have made a login form. i am using ms access as my database. i have one field as user status and i want to set user status as 1 for login and 0 for logout. that means when user logged in the application his status in that user status column should be set as 1 and as soon as he logged out his status should be set to 0. plz help me out as i am new in programming.
Kirtan PatelPosted Aug 24, 2009, 9:08 AM
Here i have Created Two Forms Form1 Which is MDI main Form and Another is LoginForm
Code for Login Form Login Button is below
I have Created Setting "LoggedInUser" in Project>Properties>Settings to Store Currently LoggedIn Username so that we can get username while logging out .
though if you dont Understand I have Included Complete Project Files(With Database) I have Coded .Link is at Bottom of the post :)
private void btnLogin_Click(object sender, EventArgs e)
{
//Get username and passwod in String From TextBoxes
string Username = txtUserName.Text.Trim();
string Password = txtPassword.Text.Trim();
//Open Connection
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
//Fire Query
SqlCommand comm = new SqlCommand("Select * from tblUserLogin where username=@username and Userpwd=@password", con);
comm.Parameters.AddWithValue("@username",Username);
comm.Parameters.AddWithValue("@password",Password);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
// if any record Found Give access to User
if (dt.Rows.Count > 0)
{
//Update The Status of user in Database
comm = new SqlCommand("update tbluserLogin set userstatus=1 where username=@username", con);
comm.Parameters.Clear();
comm.Parameters.AddWithValue("@username", Username);
comm.ExecuteNonQuery();
con.Close();
//Write CurrentlyLogged UserName in Settings
Properties.Settings.Default.CurrentLogedUser = Username;
//Open Main MDI Form
Form1 f1 = new Form1();
f1.Show();
// Make TextBoxes Blank
txtUserName.Text = "";
txtPassword.Text = "";
//Hide the Login Window Dont Close it if you Close it It will Close Whole application
this.Hide();
}
else
{
//Show Message If User have given False username and password
MessageBox.Show("Access Denied!!Enter Correct Password");
}
}
private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
{
string LoggedInUser = Properties.Settings.Default.CurrentLogedUser.ToString();
//Open Connection
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();
//Update The Status of user in Database When Logout
SqlCommand comm = new SqlCommand("update tbluserLogin set userstatus=0 where username=@username", con);
comm.Parameters.Clear();
comm.Parameters.AddWithValue("@username", LoggedInUser);
comm.ExecuteNonQuery();
con.Close();
//Show LoginForm again
Application.OpenForms["LoginForm"].Show();
//Close the MDI Form
this.Close();
}
Download Project Files
Vandana SardanaPosted Aug 24, 2009, 8:20 AM
Kirtan PatelPosted Aug 24, 2009, 6:43 AM
Tell Me the Column Names of Your table Having this information i will provide you the code
Vandana SardanaPosted Aug 24, 2009, 4:15 AM
Niradhip ChakrabortyPosted Aug 24, 2009, 2:25 AM