Hi can anyone help me tin asp.net. i want to make a sign up form with linking without returning back. if login successfully then go on welcome page otherwise login fails. just help me plzzz.
SheSaidImaPregy 28 Veteran Poster
Would you like to use Microsoft Membership or have your own custom login? For a custom login, look at below. This sub relies on two textboxes and a submit button. The text boxes are the username and password. This is with an Odbc Database connection. If you use a MySQL connection, change the lines below (which I gave to you).
Sub btnLogin_Click(S As Object, E As EventArgs)
Dim conLogin As OdbcConnection
Dim cmdSelectLoginfo As OdbcCommand
Dim dtrReaderLogin As OdbcDataReader
Dim conStringLogin As String
Dim SQLString As String
Dim strUAID As String
conStringLogin = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString")
conLogin = New OdbcConnection( conStringLogin )
SQLString = "SELECT UserID FROM Users WHERE UserName=? AND UserPassword=?"
'MySQL: SQLString = "SELECT UserID FROM Users WHERE UserName=@userName AND UserPassword=@userPass"
cmdSelectLoginfo = New OdbcCommand( SQLString, conLogin )
cmdSelectLoginfo.Parameters.Add("?userName", (txtUsername.Text.Trim()).ToString())
'MySQL: cmdSelectLoginfo.Parameters.Add("@userName",(txtUsername.Text.Trim()).ToString())
cmdSelectLoginfo.Parameters.Add("?userPass", (txtPassword.Text.Trim()).ToString())
'MySQL: cmdSelectLoginfo.Parameters.Add("@userPass",(txtUsername.Text.Trim()).ToString())
conLogin.Open()
dtrReaderLogin = cmdSelectLoginfo.ExecuteReader()
if dtrReaderLogin.hasrows then
dtrReaderLogin.Close()
strUAID = cmdSelectLoginfo.ExecuteScalar()
Session("UAID") = strUAID
conLogin.Close()
Session("Login") = "Logged"
Response.Redirect ("/loggedin.aspx")
else
Session("Login") = "Failed"
Session("UAID") = ""
conLogin.Close()
dtrReaderLogin.Close()
Response.Redirect ("/loggedin.aspx?log=fail")
'or have this post back and enable a label field with lblname.Visible = true and lblname.Text = "login failed try again."
end if
End Sub
sandeep.thakur1 -2 Newbie Poster
Quite complex one. Can be done in 8 line code.
geetajlo -2 Junior Poster in Training
But How Sandeep
SheSaidImaPregy 28 Veteran Poster
I do it this way for myself as it one, looks cleaner, and two, is easier for me to debug.
The way he is talking about is that you can set almost all your values while at Dim, like below:
Dim conLogin As New OdbcConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString"))
this specific string reduces the code above by 4 lines, but people just have their own ways of doing it as it suits them better.
You can also elminite the parameters added by just including them directly into the SQL query like:
Dim cmdSelectLoginfo As New OdbcCommand( "SELECT UserID FROM Users WHERE UserName=" & ((txtUsername.Text.Trim()).ToString()) & " AND UserPassword=" & ((txtPassword.Text.Trim()).ToString()), conLogin )
this line reduces code by 5 lines. Use what is best for you for debugging. After making sure it works the way you desire, worry about using less code as it saves bandwidth and load time (but nothing you will most likely have to worry about as it is minimal unless you are dealing with capacities of millions of unique visitors each month.)
sandeep.thakur1 -2 Newbie Poster
It in VB and give best performance.
In C# i am posting later.
Just add this code. if any query revert.
Sandeep
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If Page.IsValid Then
If DBFunction(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
Session("UserName") = txtUserName.Text
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False)
Else
lblMessage.Text = "Invalid Login!"
lblMessage.ForeColor = Drawing.Color.Red
End If
End If
End Sub
'Add thi function to connect to database.
Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
Dim MyCmd As New SqlCommand("sp_ValidateUser", objConnection)
MyCmd.CommandType = CommandType.StoredProcedure
Dim objParam1, objParam2 As SqlParameter
Dim objReturnParam As SqlParameter
objParam1 = MyCmd.Parameters.Add("@UserName", SqlDbType.VarChar)
objParam2 = MyCmd.Parameters.Add("@Password", SqlDbType.VarChar)
objReturnParam = MyCmd.Parameters.Add("@Num_of_User", SqlDbType.Int)
objParam1.Direction = ParameterDirection.Input
objParam2.Direction = ParameterDirection.Input
objReturnParam.Direction = ParameterDirection.ReturnValue
objParam1.Value = txtUserName.Text
objParam2.Value = txtPassword.Text
Try
If objConnection.State = ConnectionState.Closed Then
objConnection.Open()
MyCmd.ExecuteNonQuery()
End If
If objReturnParam.Value < 1 Then
lblMessage.Text = "Invalid Login!"
lblMessage.ForeColor = Drawing.Color.Red
Else
Return True
End If
objConnection.Close()
Catch ex As Exception
lblMessage2.Text = "Error Connecting to Database!" & ex.Message
lblMessage2.ForeColor = Drawing.Color.Red
End Try
End Function
Add Stored Procedure in you database QUERY ANALYZER:
CREATE PROCEDURE sp_ValidateUser (
@UserName VARCHAR(50) = NULL,
@Password VARCHAR(50) = NULL,
@Num_of_User INT = 0
)
AS
SET @Num_of_User = (SELECT COUNT(*) AS Num_of_User
FROM Members
WHERE UserName = @UserName AND Password = @Password)
RETURN @Num_of_User
you have to name you button btnSubmit
and textboxes
txtUserName
txtPassword
sandeep.thakur1 -2 Newbie Poster
Thi VB Code wil automatically search for Default.aspx page. If Default.aspx is not in your poject, it will give error.
For session, To display Hi Sandeep ! , like this, u have to take a label on default and set
label1.text="Hi " & Session[UserName] & "!"
SheSaidImaPregy 28 Veteran Poster
Doing it this way you also need to set authentication in your web.config. So if you come up with errors, check there first.
sandeep.thakur1 -2 Newbie Poster
SheSaidImaPregy is right !
I forgot to add connection string,that you have to define in your web.config
or do like this in your page
Partial Class Login
Inherits System.Web.UI.Page
[U]Dim objConnection As New SqlConnection("Data Source=SANDEEPTHAKUR\SANDY;database=Northwind;Integrated Security=SSPI")[/U] Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
SheSaidImaPregy 28 Veteran Poster
SheSaidImaPregy is right !
I forgot to add connection string,that you have to define in your web.config
or do like this in your pagePartial Class Login Inherits System.Web.UI.Page [U]Dim objConnection As New SqlConnection("Data Source=SANDEEPTHAKUR\SANDY;database=Northwind;Integrated Security=SSPI")[/U] Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub
Yeah, but if you use this connection more than once (which I am sure of it), then define it in your web.config.
enjoy! :)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.