How to Create Login Window in C# using SQL Server Database in Visual Studio?
Hello Everyone, If you are planning to study C# programming for your project or if you are interested to learn C# Programming, Then, i am going to write one article about the C# related project (a small project/application) using Window Form Application in Visual Studio which is the very basic but very important guideline for you. But this will be very important project for you if you do not try before. As you all know C# is an Event Driven Programming Language this features enable us to drag and drop some controls like button, textbox, labels etc directly rather than writing code for them and this actually save your time when you are programming and also make us easy to program. This project will show you how to drag and drop controls from tools into your window form and how to connect your application to database which is the important and most basic knowledge you should know when you are making a complex system using C#.
So, i am going to give you demonstration about how to create login window using C# which is based on windows form application through the video. So, please do watch following video for this little but very important project.
First of all you need to have Visual Studio and SQL Server Studio. you all can download these software from microsoft official website.
SQL Server: From Here
Visual Studio: From Here
STEPS:
[sociallocker]
1. Open your visual studio of any version and then go to file new and then click on project. Then new window will appear and from the left pane select visual c#. under visual c# select window and then select window form application from the middle pane and give the name of your project and click ok.
2. Drag and drop two label, two textbox and one button from the toolbox and change the text and name of these control from the properties of these controls.
3. Now from the left pane select server explorer and right click on data connection and click on add connection. After clicking on add connection new window will appear and you will see the Data Source option and Database file name (new or existing); set the Data Source to Microsoft SQL Server Database File(SqlClient ) and give any name in the database file name section and then click ok, new window will appear as below:
Then click yes, after clicking yes, server explorer will look like the following one:
4. Then right click on table>Add new Table, add username and password in the name section and choose the data type and then click on the update icon.
you can also change the name of the table from the SQL Command shown just below the this table and then you should click on update button.
5. Now select you database in my case testlogin.mdf from the server explorer and go to properties, from the properties window copy the connection string.
6. Now you need to add one namespace as:
using System.Data.sqlclient;
7. Then go to the design section of the login window and double click on the button and write following code inside that button.
Note: change the connection string and SQL Statement (Table name and database name) based on your specification.
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\RANBAH~1\Documents\testlogin.mdf;Integrated Security=True;Connect Timeout=30"); SqlDataAdapter sda = new SqlDataAdapter("select count(*) from login where username ='" + textBox1.Text + "' and password='" + textBox2.Text + "'", conn); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows[0][0].ToString() == "1") { this.Hide(); Main mm = new Main(); mm.Show(); } else { MessageBox.Show("please enter correct username and password", "alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
[/sociallocker]
Watch the video for the full guideline!