Login with store procedure in ASP.Net MVC using VB.Net

satabeach
 
on May 13, 2022 11:02 PM
Sample_177072.zip
433 Views

How to create login with store procedure in MVC vb.net?

I have checked the internet, there are so many using C#.

But I want in vb.net, mainly in HTML part.

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on May 14, 2022 02:56 AM

Hi sarabeach,

Please refer below sample.

SQL

CREATE TABLE [UserLogin]
(
    [UserName] VARCHAR(20)
    ,[Password] VARCHAR(20)
)
GO
INSERT INTO [UserLogin] VALUES('MudassarKhan', '1234')

GO

CREATE PROCEDURE [LoginPage]
    @Username VARCHAR(20)
    ,@Password VARCHAR(20)
AS 
BEGIN
    SET NOCOUNT ON;
    SELECT COUNT(*)
    FROM  [UserLogin] 
    WHERE [Username] = @Username
    AND [Password] = @Password
END

Controller

Public Class HomeController
    Inherits Controller

    ' GET: Home
    Function Index() As ActionResult
        Return View()
    End Function

    <HttpPost>
    Function Index(username As String, password As String) As ActionResult
        Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(conString)
            Using cmd As SqlCommand = New SqlCommand("LoginPage", con)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@Username", username)
                cmd.Parameters.AddWithValue("@Password", password)
                con.Open()
                Dim result As Integer = Convert.ToInt32(cmd.ExecuteScalar())
                If (result > 0) Then
                    Return RedirectToAction("Home")
                End If
                con.Close()
            End Using
        End Using

        Return View()
    End Function

    Function Home() As ActionResult
        Return View()
    End Function
End Class

View

@Code
    Layout = Nothing
End Code

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @Using Html.BeginForm("Index", "Home", FormMethod.Post)
        @<input type="text" name="username" />
        @<input type="password" name="password" />
        @<input type="submit" value="Login" />
    End Using
</body>
</html>

Screenshot