This Login Code in C# with SQL Server shows how to connect to SQL Server and make a login form in C# using Visual Studio and SQL Server Database as the project’s back-end.
Furthermore, with this simple tutorial, you can learn how to create a login system in C# step by step and get the source code. In this tutorial, you can also download the source code for free.
Details and Technology
Project Name: | Login Code in C# with SQL Server Free Source Code |
Abstract : | Login Code in C# is a website page or website entry page that asks users to sign in and identify themselves. |
Project Type: | Desktop Application |
Technology : | C# Visual Studio 2022 with C# Language |
Database : | SQL-Server 2021 |
Developer: | Source Code Hero |
Make sure you have Microsoft Visual Studio and SQL Server on your computer before you start making this Login Page in C#.
What is the importance of log in form?
A Login form is where you enter your authentication information to get to a page or form that only certain people can see. There is a field for the username and another for the password on the login form. When the login form is sent, the Login Code behind it checks the credentials to make sure they are real. If they are, the user can go to the restricted page.
Why is a secure Login Code in C# important?
They need secure login pages so that people outside of the organization or users who haven’t signed up can’t get in. If the login page is secure, hackers won’t be able to find weak spots and won’t be able to attack.
Basic Steps on How to Start the Login Code in C# with SQL Server Project
Time needed: 5 minutes.
- Step 1: Open SQL Server
Type in the Server Name, Username, and Password, then click the “Connect” button.
- Step 2: Make a new database.
Right-click on Database, then choose New Database.
- Step 3: Give it a name.
Type “Login” as the name of the database, then click “OK.”
- Step 4: Make a new table.
Explore the Login database, then right-click on Tables and select New Table.
- Step 5: Make the table look nice.
Figure: Give the columns names and set the data types as shown.
- Step 6: Open Visual Studio
Select File->New->New Project.
- Step 7: Choose Windows Form App.
After that, choose Windows Form App (.NET Framework).
- Step 8: Create name of project.
Then give your project a name, and click the “Create” button.
- Step 9: Copy the code that is shown below.
Last, copy and paste all of the code below into your file.
This is the Full Login Code in C# with SQL Server
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Login_System
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=LAPTOP-AMM1MQ8C;Initial Catalog=Login;Integrated Security=True";
con.Open();
string userid = textBox1.Text;
string password = textBox2.Text;
SqlCommand cmd = new SqlCommand("select UserName,Password from LoginMst where UserName='" + textBox1.Text + "'and Password='" + textBox2.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Login sucess Welcome to Homepage https://itsourcecode.com");
System.Diagnostics.Process.Start("https://itsourcecode.com");
}
else
{
MessageBox.Show("Invalid Login please check username and password");
}
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=LAPTOP-AMM1MQ8C;Initial Catalog=Login;Integrated Security=True");
con.Open();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
This article for Login Code in C# is having project that is open source wherein you can download the source code for free. Just click the button below and your good to go.