In this post i am going to explain a simple login application in windows c# . In this App we will cover few concepts like login concept with database connection, working with multiple forms in windows c# , passing values from one form to another form , adding local database to our windows c# project .
For this i am designing two forms one is for Login and another is for displaying Welcome Message for logged in user .
The screenshots of application shown below .
We are designing the two forms as shown above . and we are designing a table LoginDetails in LoginDB , which is a Local Database added to our application .
Table : LoginDetails
sql query to create table :
CREATE TABLE LOGINDETAILS( NAME VARCHAR(25),PASSWORD NVARCHAR(25))
Now the C# code of Form1 is like this
Download Source Code
Posts you may like :
>>> Simple Registration form in Asp.Net with validations
>>> Registration form in Asp.Net / C#.Net uisng 3-tier architecture
For this i am designing two forms one is for Login and another is for displaying Welcome Message for logged in user .
The screenshots of application shown below .
We are designing the two forms as shown above . and we are designing a table LoginDetails in LoginDB , which is a Local Database added to our application .
Table : LoginDetails
sql query to create table :
CREATE TABLE LOGINDETAILS( NAME VARCHAR(25),PASSWORD NVARCHAR(25))
Now the C# code of Form1 is like this
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace LoginApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string msg;
//SqlConnection con = new SqlConnection("Data
Source=LocalHost;Integrated Security=true;Initial Catalog=sameer");
/** the connection string below is for connecting
localdatabase LoginDB.mdf exists in the project **/
SqlConnection con = new
SqlConnection("Data
Source=.\\SQLExpress;User Instance=true;Integrated
Security=true;AttachDbFilename=|DataDirectory|lOGINdb.mdf");
SqlCommand cmd;
DataSet ds = new DataSet();
private void
btnlogin_Click(object sender, EventArgs e)
{
try
{
if (txtname.Text == ""
|| txtpassword.Text == "")
{
MessageBox.Show(" Enter UserName and Password .");
return;
}
cmd
= new SqlCommand("SELECT * FROM LoginDETAILS where Name='"
+ txtname.Text + "' and Password='"
+ txtpassword.Text + "'", con);
SqlDataAdapter da = new
SqlDataAdapter(cmd);
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i == 1)
{
msg = "Welcome " +
txtname.Text;
this.Hide();
Form2 f2 = new
Form2(msg);
f2.Show();
ds.Clear();
}
else
{
MessageBox.Show("Not Registered User or Invalid Name/Password");
txtpassword.Text = "";
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void
btnRegister_Click(object sender, EventArgs e)
{
try
{
if (txtname.Text == ""
|| txtpassword.Text == "")
{
MessageBox.Show(" Enter UserName and Password .");
return;
}
/** checking whether name exists **/
cmd
= new SqlCommand("SELECT * FROM LoginDETAILS where Name='"
+ txtname.Text + "'", con);
SqlDataAdapter da = new
SqlDataAdapter(cmd);
da.Fill(ds); //filling dataset
int i = ds.Tables[0].Rows.Count; //checking rows count in dataset
if (i > 0)
{
MessageBox.Show("UserName "+txtname.Text+" Already Exists..");
txtpassword.Text = "";
ds.Clear(); //clearing dataset
}
else
{
/** inserting name and password in table
logindetails **/
cmd = new SqlCommand("INSERT INTO LOGINDETAILS VALUES('"+txtname.Text+"','"+txtpassword.Text+"')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
msg = "Registered Successfully \n
Welcome "+txtname.Text;
this.Hide(); //hiding
form1
Form2 f2 = new
Form2(msg);
f2.Show(); //showing form2
}
}
catch (Exception
ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
}
}
and the c# code of form2 is like this
using System.Windows.Forms;
namespace LoginApplication
{
public partial class Form2 : Form
{
public Form2(string
msg) //passing
parameter
{
InitializeComponent();
lblWelcome.Text =msg;
}
private void
lblLogOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide(); //hiding form2
Form1 f1 = new Form1();
f1.Show(); //showing form1
}
}
Download Source Code
Posts you may like :
>>> Simple Registration form in Asp.Net with validations
>>> Registration form in Asp.Net / C#.Net uisng 3-tier architecture
No comments:
Post a Comment
Thanks for your comments.
-Sameer