Let's we see how to create a login page in C#. Beginners can easily create a login page in C# using the following procedure.
Procedure:
- Create a database in SQL Server 2005
- Create a C# project
- Connect the created database with the created C# project
- Generate code
- Check the Login page (Testing)
Step 1: Create a database in SQL Server 2005
Provide your correct local server name, Username and word
![]()
Then you can see the following window.
Use the following procedure:
Provide the database name as Login.
![]()
![]()
Now the database has been created successfully. The following steps will show the table creation in SQL Server. Double-click the database. Now you can see the following window and select the table.
![]()
Column Creation
![]()
![]()
Provide a name for the table.
![]()
Created Table
![]()
Open the table.
![]()
Entering contents into the table.
![]()
Step 2: Create a C# project
Open Visual Studio.
![]()
Create a new project.
![]()
Choose Visual C# Windows application.
![]()
Provide the project name.
![]()
![]()
Place controls onto the form.
![]()
![]()
Now the project has been created...!
Step 3: Connect the created database with the created C# project.
![]()
Provide the correct servername, username and word. The servername, username and word are all the same as for SQL Server 2005.
Choose "Use SQL Server Authentication".
After entering a correct servername, username and word you can choose the database from the list of databases.
![]()
Choose the Login Database.
![]()
Check the Connection.
![]()
![]()
Step 4: Generate Code
Add the namespace: "using System.Data.SqlClient;".
![]()
The connection String is necessary for the connection to the database with our project.
![]()
Click the server explorer and then click the login database.
Now you can see the connection string in the properties window:
![]()
Copy the connection string and paste it into the code window.
Now click F5 to check the database connection.
![]()
![]()
Now copy the following code and paste it with in the button click method.
if (textBox1.Text == "" || textBox2.Text == "")
{
}
else
{
SqlCommand scmd;
SqlDataReader sdr;
scmd = new SqlCommand("select Username,word from tbl_Check where USername=@Username and word=@word",sc);
scmd.Parameters.AddWithValue("@Username",textBox1 .Text .ToString ());
scmd.Parameters.AddWithValue("@word",textBox2 .Text .ToString ());
sdr = scmd.ExecuteReader();
if (sdr.HasRows)
{
sdr.Close();
MessageBox.Show("Welcome- The Username and word is Correct", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Invalid Username or word", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Step 5: Check the Login page (Testing)
Provide the correct username and word that is already stored in the tbl_Check table.
![]()
Provide the wrong username and word.
![]()
Beginners can use this to create a simple Login page.