How to Create Login Form in Visual Studio and Connect With SQL Server

The following procedure shows how to create a login form in Visual Studio and connect with SQL Server in 10 steps.

Step 1

Open any version of Visual Studio  you have installed in your machine; I have Visual Studio  10. When open it will look like this:

open visual studio

Step 2

Click on the middle tab of new project. When you click it will look like this:

create new project in visual studio

Step 3

In Installed Templates the first option is Visual C# language and then select Windows Forms application.

In the following see the Name label and already show the default name WindowFormsApplication7.

You can change the name as you wish or as per program creation you can mention and click and OK.

Step 4

After clicking OK a simple Windows Forms form will look like:

window form application

Step 5

In your Windows Forms form set the size as needed. You can set the size and if you want to set the size by properties then right-click on the Windows Forms form and in the last option of show properties click to see the Windows Forms form properties.

window form preporty

Set the Windows Forms form name and show text in the specified properties. If you wish you can set the form1 name; my form1 name is “Log in Form”.

Click the Toolbox and drag and drop two buttons and two labels and two TextBoxes as in the following:

textbox and button

Step 6

Then you need to change the lable1 name to User Name and label2 to Password then for the Buttons change the button1 right-click property to show text select button1 and type &Log In and button2 &Cancel. where & is used for a small Underline only one first text look up. See the image button1 to show the Log In and button2 show Cancel for style .

Step 7

Now drag all text boxes and labels and buttons and arrange all the tools.

Then right-click each tool one by one and set the properties for font and color. Look at the following:

set font and color

All sets of fonts and font size and if you want to set the image background set and form icon also you can set and more things and tab index set serially for all tool of labels, TextBox and buttons.

textbox and buttons on forn

Now the final design looks like this type.

Step 8

To connect with a SQL Server database right-click on the form and click on properties then go to the upper second option (Data Binding). In Data Binding click the text area and it will look like:

Data Binding option in visual studio

Now click on Add project data Source.

Step 9

Then open these windows select “Database” and click Next then select “Dataset” and click Next again select “Dataset” and click Next then click on New connection then click the Refresh Button then select your SQL Server name. For example my SQL name is KRISHNA-PC\SQLEXPRESS for the select radio button select or enter a database name for example my database name STUDENT then click Test Connection and click the OK Button.

Click the connection string to show this type of connection string then select and copy then click on "Next" again click Next tick table and the finish button now see the window:

database connection

This is SQL Server 2008 Database Details and show here the userid and Password. You use only these three passwords for this login form.

Database Details

Step 10

This is the coding part of this application below:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.Sql;  
  10. using System.Data.OleDb;  
  11. using System.Data.SqlClient;  
  12.   
  13. namespace login_form  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         SqlConnection con = new SqlConnection();  
  18.         public Form1()  
  19.         {  
  20.             SqlConnection con = new SqlConnection();  
  21.             con.ConnectionString = "Data Source=KRISHNA-PC\\SQLEXPRESS;Initial Catalog=STUDENT;Integrated Security=True";  
  22.   
  23.             InitializeComponent();  
  24.         }  
  25.   
  26.         private void Form1_Load(object sender, EventArgs e)  
  27.         {  
  28.             // TODO: This line of code loads data into the 'sTUDENTDataSet.login' table. You can move, or remove it, as needed.  
  29.             //this.loginTableAdapter.Fill(this.sTUDENTDataSet.login);  
  30.             SqlConnection con = new SqlConnection("Data Source=KRISHNA-PC\\SQLEXPRESS;Initial Catalog=STUDENT;Integrated Security=True");  
  31.             con.Open();  
  32.   
  33.             {  
  34.             }  
  35.         }  
  36.   
  37.         private void button1_Click(object sender, EventArgs e)  
  38.         {  
  39.             SqlConnection con = new SqlConnection();  
  40.             con.ConnectionString = "Data Source=KRISHNA-PC\\SQLEXPRESS;Initial Catalog=STUDENT;Integrated Security=True";  
  41.             con.Open();  
  42.             string userid = textBox1.Text;  
  43.             string password = textBox2.Text;  
  44.             SqlCommand cmd = new SqlCommand("select userid,password from login where userid='" + textBox1.Text + "'and password='" + text            Box2.Text + "'", con);  
  45.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  46.             DataTable dt = new DataTable();  
  47.             da.Fill(dt);  
  48.             if (dt.Rows.Count > 0)  
  49.             {  
  50.                 MessageBox.Show("Login sucess Welcome to Homepage http://krishnasinghprogramming.nlogspot.com");  
  51.                 System.Diagnostics.Process.Start("http://krishnasinghprogramming.blogspot.com");  
  52.             }  
  53.             else  
  54.             {  
  55.                 MessageBox.Show("Invalid Login please check username and password");  
  56.             }  
  57.             con.Close();  
  58.         }  
  59.   
  60.         private void button2_Click(object sender, EventArgs e)  
  61.         {  
  62.             Application.Exit();  
  63.   
  64.         }  
  65.     }  
  66. }  
So friends this article will help you to create a Login Form in Visual Studio and connect with SQL Server. The next time I will put some more interesting commands, thank you. I hope this is helpful for you. Enjoy :).

 

Up Next
    Ebook Download
    View all
    Learn
    View all