How to make login system in ASP using VB code

By Guest | Nov 11, 2021 | ASP NET
Share :

https://www.fundaofwebit.com/post/login-system-in-asp-using-vb-code

How to make login system in ASP using VB code

We will design the login form using bootstrap v5. Code below:

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6 mt-5">
            <div class="card shadow">
                <div class="card-header text-white bg-primary">
                    <h4>Login</h4>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label for="exampleInputEmail1" class="form-label">Email address</label>
                        <asp:TextBox ID="email" CssClass="form-control"  runat="server" TextMode="Email"></asp:TextBox>
                    </div>
                    <div class="mb-3">
                        <label for="exampleInputPassword1" class="form-label">Password</label>
                        <asp:TextBox ID="password" CssClass="form-control" runat="server" TextMode="Password"></asp:TextBox>
                    </div>
                    <asp:Button ID="login_btn" runat="server" CssClass="btn btn-primary" Text="Submit" />
                </div>
            </div>
        </div>
    </div>
</div>
   

On button click, we will fetch the value from the textbox and check if there is any user registered (records) using the given email and password, using the below code.

Protected Sub login_btn_Click(sender As Object, e As EventArgs) Handles login_btn.Click

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader

    con.ConnectionString = "YOUR_CONNECTION_STRING_HERE"
    con.Open()

    cmd.Connection = con
    cmd.CommandText = "SELECT * FROM users WHERE email='" & email.Text & "' and password='" & password.Text & "' "

    dr = cmd.ExecuteReader
    If dr.HasRows Then
        MsgBox("Logged in Successfully", MsgBoxStyle.Information, "Success")
        Session("auth") = True
        con.Close()
    Else
        MsgBox("Invalid Username or Password", MsgBoxStyle.Critical)
        Response.Redirect("login.aspx")
        con.Close()
    End If

    con.Close()

End Sub

Thank you.

https://www.fundaofwebit.com/post/login-system-in-asp-using-vb-code

Share this blog on social platforms