0% found this document useful (0 votes)
217 views

Imports System - data.SqlClient Imports System - Data Imports System Partial

This code defines a login function that connects to a SQL database using a stored procedure called "sp_login". It takes in email and password parameters, executes the stored procedure, and returns a boolean value indicating whether the login was successful based on the return value from the stored procedure. It handles opening and closing the database connection, adding the necessary parameters to the command object, and displays an error message if the login fails.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views

Imports System - data.SqlClient Imports System - Data Imports System Partial

This code defines a login function that connects to a SQL database using a stored procedure called "sp_login". It takes in email and password parameters, executes the stored procedure, and returns a boolean value indicating whether the login was successful based on the return value from the stored procedure. It handles opening and closing the database connection, adding the necessary parameters to the command object, and displays an error message if the login fails.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Imports System.Data.

SqlClient
Imports System.Data
Imports System
Partial Class Login
Inherits System.Web.UI.Page

Sub btnSubmit_OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handl

If Page.IsValid Then ' ||||| Meaning the Control Validation was successful!
Session("Email") = txtEmail.Text

If DBConnection(txtEmail.Text.Trim(), txtPassword.Text.Trim()) Then


Response.Redirect("default.aspx")
Else
Response.Redirect("faile.aspx")

End If
End If
End Sub
Function DBConnection(ByVal strEmail As String, ByVal strPassword As String) As Boolean
'<sumamry>
' ||||| Declare Required Variables
' ||||| Access appSettings of Web.Config for Connection String (Constant)
'</summary>
' ||||| This is the Connections Object for an SQL DB
Dim myConnection As New SqlConnection(System.Configuration.ConfigurationManager.App
Dim myCommand As New SqlCommand("sp_login", myConnection)
' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
' ||||| Create Parameter Objects for values passed in
Dim objEmail, objPassword As SqlParameter
' ||||| Create a parameter to store your Return Value from the Stored Procedure
Dim objReturnParam As SqlParameter
'Dim objReturnParam1 As SqlParameter
'Dim objReturnParam2 As SqlParameter
'Dim objReturnparam3 As SqlParameter
'<sumamry>
' ||||| Add the parameters to the parameters collection of the
' ||||| command object, and set their datatypes (OleDbType in this case)
'</summary>
objEmail = myCommand.Parameters.Add("@Email", SqlDbType.VarChar)
objPassword = myCommand.Parameters.Add("@Password", SqlDbType.VarChar)
objReturnParam = myCommand.Parameters.Add("@return_value", SqlDbType.Int)
'objReturnParam1 = MyCmd.Parameters.Add("@ruserid", SqlDbType.NVarChar)
'objReturnParam2 = MyCmd.Parameters.Add("@raccessid", SqlDbType.NVarChar)
'objReturnparam3 = MyCmd.Parameters.Add("@remail", SqlDbType.NVarChar)
' ||||| Set the direction of the parameters...input, output, etc
objEmail.Direction = ParameterDirection.Input
objPassword.Direction = ParameterDirection.Input
objReturnParam.Direction = ParameterDirection.ReturnValue ' Note RETURNVALUE
'objReturnParam1.Direction = ParameterDirection.ReturnValue ' Note RETURNVALUE
'objReturnParam2.Direction = ParameterDirection.ReturnValue ' Note RETURNVALUE
'objReturnparam3.Direction = ParameterDirection.ReturnValue ' Note RETURNVALUE
'' ||||| Set the value(s) of the parameters to the respective source controls
objEmail.Value = txtEmail.Text
objPassword.Value = txtPassword.Text
' ||||| Try, catch block!
Try
' ||||| Check if Connection to DB is already open, if not, then open a conn
If myConnection.State = ConnectionState.Closed Then
' ||||| DB not already Open...so open it
myConnection.Open()
myCommand.ExecuteNonQuery()
End If
' ||||| Was the return value greater than 0 ???
If objReturnParam.Value < 1 Then
lblMessage2.Text = "Invalid Login!"
Else
Return True
End If
' ||||| Close the Connection Closes with it
myConnection.Close()

Catch ex As Exception
lblMessage2.Text = "Error Connecting to Database!"
End Try

End Function
End Class

You might also like