Simple Login Application in Windows C#
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 .
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
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 = newSqlConnection("Data Source=.\SQLExpress;User Instance=true;Integrated Security=true;AttachDbFilename=|DataDirectory|lOGINdb.mdf"); SqlCommand cmd; DataSet ds = new DataSet(); private voidbtnlogin_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 = newSqlDataAdapter(cmd); da.Fill(ds); int i = ds.Tables[0].Rows.Count; if (i == 1) { msg = "Welcome " + txtname.Text; this.Hide(); Form2 f2 = newForm2(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 voidbtnRegister_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 = newSqlDataAdapter(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 = newForm2(msg); f2.Show(); //showing form2 } } catch (Exceptionex) { MessageBox.Show(ex.Message); con.Close(); } } } } |
and the c# code of Form2 is like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System.Windows.Forms; namespace LoginApplication { public partial class Form2 : Form { public Form2(stringmsg) //passing parameter { InitializeComponent(); lblWelcome.Text =msg; } private voidlblLogOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { this.Hide(); //hiding form2 Form1 f1 = new Form1(); f1.Show(); //showing form1 } } |