Login System in C# and SQL Server 2005
In this tutorial, I will teach you how to create a Login System in C#.net and SQL server 2005. This is a cool and simple Login System that could help you secure your system from invaders. It covers features that would make the login system user-friendly and contains step by step guide that is so easy to follow. So, in the future, you will know how to build an application that features a login system.
But if you want to start how to code using visual studio 2019, you can start your lesson here on how to connect Mysql to visual Studio 2019.
Let’s get started:
Create a database and name it “employeedb”.
After creating the database, open Microsoft Visual Studio and create new Windows Form Application. Then do the following design of a Form as shown below.
Go to the Solution Explorer, double click the “View Code” to display the code editor.
Note: Put using System.Data.SqlClient; above the namespace to access sql server library.
In the code editor, declare all the classes and variables that are needed.
//initialize all classes SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable(); //declaring variables string query; int result, maxrow;
After declaring the classes and variables, establish a connection between SQL server 2005 and C#.net in the first load of the Form.
private void Form1_Load(object sender, EventArgs e) { //set a connection between SQL server and Visual C# conn.ConnectionString = "Data Source=.\\SQLEXPRESS;database=employeedb;trusted_connection=true;"; }
After that, go back to the design view, double click the button and do the following code in the method.
private void btnLogin_Click(object sender, EventArgs e) { try { conn.Open(); //create a query for retrieving data in the database. query = "SELECT * FROM tbluser WHERE UNAME='" + txtUsername .Text + "' and PASS='" + txtPassword .Text +"'"; //initialize new Sql commands cmd = new SqlCommand(); //hold the data to be executed. cmd.Connection = conn; cmd.CommandText = query; //initialize new Sql data adapter da = new SqlDataAdapter(); //fetching query in the database. da.SelectCommand = cmd; //initialize new datatable dt = new DataTable(); //refreshes the rows in specified range in the datasource. da.Fill(dt); //getting the total row in the table. maxrow = dt.Rows.Count; //validating the total rows in the table if (maxrow > 0) { //popup message MessageBox.Show("Welcome " + dt.Rows[0].Field<string>("UROLE")); //disable the group box. groupBox1 .Enabled = false ; //store the value in the linklabel lblGuest.Text = "Hello " + dt.Rows[0].Field<string>("NAME") + " Logout"; //clearing textboxes txtPassword.Clear(); txtUsername.Clear(); } else { //when account does not exist, it will display this message. MessageBox.Show("Account does not exist."); } } catch (Exception ex) { //catching error MessageBox.Show(ex.Message); } finally { da.Dispose(); conn.Close(); } }
Do the following method for the logout of the “Login” system.
private void lblGuest_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //logout process.. //the form will return to the Guest view. lblGuest.Text = "Hello Guest!"; groupBox1.Enabled = true; txtPassword.Clear(); txtUsername.Clear(); }
Finally, do the following method for the “close” button.
private void btnClose_Click(object sender, EventArgs e) { //closing the form. this.Close(); }
Output:
For Inquiries
If you have any questions or suggestions about this System, please leave a comment below.
For all students who need a programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :
Email – [email protected]
Mobile No. – 09305235027 – tnt
Howdy I am so excited I found your webpage, I really found you by mistake, while I was searching on Askjeeve for something else, Anyways I am here now and would just like to say many thanks for a tremendous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to read it all at the moment but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read much more, Please do keep up the great work.
If you would like to get much from this piece of writing then you have to apply such methods to your won weblog.
This is a really good tip particularly to those fresh to the blogosphere. Brief but very precise information… Many thanks for sharing this one. A must read article!
Thanks, I really appreciate that.
Hi my family member! I want to say that this article is amazing, nice written and include almost all important infos. I’d like to peer extra posts like this .
I got this site from my buddy who shared with me on the topic of this website and at the moment this time I am browsing this web site and reading very informative articles or reviews at this time.
I do not know whether it’s just me or if everyone else experiencing problems with your website. It appears as if some of the written text on your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them as well? This may be a problem with my web browser because I’ve had this happen previously. Cheers
I would like to thank you for the efforts you have put in penning this website. I’m hoping to view the same high-grade content by you in the future as well. In fact, your creative writing abilities has encouraged me to get my own website now 😉
Hello, i believe that i saw you visited my blog thus i came to return the desire?.I am attempting to find things to enhance my website!I suppose its ok to use some of your ideas!!
Can I simply just say what a relief to find someone that actually knows what they are discussing on the net. You certainly understand how to bring an issue to light and make it important. More and more people have to look at this and understand this side of the story. It’s surprising you are not more popular since you surely possess the gift.
You really make it seem really easy with your presentation but I find this matter to be really one thing which I feel I might by no means understand. It kind of feels too complex and extremely broad for me. I am looking forward for your next submit, I’ll try to get the hold of it!
This is a really good tip particularly to those fresh to the blogosphere. Short but very accurate information… Thanks for sharing this one. A must read post!
It is not my first time to pay a visit this site, i am visiting this website dailly and take nice information from here all the time.
hello!,I like your writing so much! share we keep in touch extra about your article on AOL? I need a specialist on this space to resolve my problem. Maybe that is you! Looking forward to look you.