Login using mssql local server on smarterasp net online website

Introduction

ASP.NET is a popular programming language used for dynamic web applications. In this article, we will explore how to implement a login functionality using MSSQL local server on the SmarterASP.NET online website.

Setting up the Database

Before we , we need to create a database on the MSSQL local server. You can use tools like SQL Server Management Studio to create a new database and a table to user credentials.


CREATE DATABASE LoginDB;
USE LoginDB;

CREATE TABLE Users (
    Id INT PRIMARY KEY IDENTITY,
    Username VARCHAR(50) NOT NULL,
    Password VARCHAR(50) NOT NULL
);

Creating the Login Page

Now, let's create a login page where users can credentials. We will use ASP.NET Web for this example.







    Login Page


    

Login

Implementing the Login Functionality

In the code-behind file (Login.aspx.cs), we will write the to authenticate the user against the database.


using System;
using System.Data.SqlClient;

namespace YourNamespace
{
     partial  Login : System.Web.UI.Page
    {
         void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;

            string connectionString = "Data Source=(local);Initial Catalog=LoginDB;Integrated Security=True";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND Password = @Password";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@Username", username);
                command.Parameters.AddWithValue("@Password", password);
                int count = (int)command.ExecuteScalar();

                if (count > 0)
                {
                    // Successful login
                    Response.Redirect("Home.aspx");
                }
                else
                {
                    // Invalid credentials
                    Response.Write("Invalid username or password.");
                }
            }
        }
    }
}

Conclusion

In this article, we have learned how to implement a login functionality using MSSQL local server on the SmarterASP.NET online website. By following the steps mentioned above, you can create a secure login system for your web application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents