How to Create Login Form in C#.NET using MySQL Database?[With Source Code]
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.
Download Wamp Server: http://www.wampserver.com/en/
if you are new to the MySQL and C# and you are gonna connect C# with MySQL for the very first time, then you need to download and install following software which are free of code.
MySQL for Visual Studio: https://dev.mysql.com/downloads/windows/visualstudio/
MySQL Connector for Visual studio: https://dev.mysql.com/downloads/connector/net/
Steps:
[sociallocker]
1. After download and installing open it, and open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty). And then click on new and create one database having any name you want and the click ok, then create table having two field and click on go. After that, you need to insert the field name like: username, password. and then select the length as well as data type as your requirement and then click on save. You can watch the video for this designing part, you may understand better in video.
2. 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.
3. Drag and drop two label, two textbox and one button and change the text and name of these control from the properties of these controls.
4. Then add the reference of MySQL. To do this, right click on reference select add reference and the click on extension then scroll down and find the MySQL.data and click on check box and click ok.
5. Go to the .cs file of your form and import namespce as:
using MySql.Data.MySqlClient;
6. 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 then you will also see the change button, click on change button and select MySQL database (This option will not appear if you are using the Visual studio of version other than professional and ultimate, to view this option you need to install this software, https://dev.mysql.com/downloads/windows/visualstudio/), Then click ok, after doing this you will see new window and there you need to proview your server name, username and password(i.e. localhost, root, the password filed is empty i.e. nothing and these value are default value, if you have set any username and password provide your own). Then click Ok.
7. Source Code: Inside the Login Button Click Event! (Double click login button and write the following code).
string user = txtuser.Text; string pass = txtpass.Text; MySqlConnection conn = new MySqlConnection("server = localhost; user id = root; database = mysqlcsharp"); MySqlDataAdapter sda = new MySqlDataAdapter("select count(*) from mysqlcshap where username = '" + txtuser.Text + "' and password = '" + txtpass.Text + "'", conn); DataTable dt = new DataTable(); sda.Fill(dt); if(dt.Rows[0][0].ToString()=="1") { MessageBox.Show("username and password are matched", "info", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("incorrect username and password", "alter", MessageBoxButtons.OK, MessageBoxIcon.Error); }
[/sociallocker]
If you have any confusion watch the following video: