| | |
Sign up form
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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).
ASP.NET Syntax (Toggle Plain Text)
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
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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.)
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.)
Last edited by SheSaidImaPregy; Oct 8th, 2007 at 11:07 am.
It in VB and give best performance.
In C# i am posting later.
Just add this code. if any query revert.
Sandeep
you have to name you button btnSubmit
and textboxes
txtUserName
txtPassword
In C# i am posting later.
Just add this code. if any query revert.
Sandeep
ASP.NET Syntax (Toggle Plain Text)
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
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
For session, To display Hi Sandeep ! , like this, u have to take a label on default and set
ASP.NET Syntax (Toggle Plain Text)
label1.text="Hi " & Session[UserName] & "!"
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
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
Dim objConnection As New SqlConnection("Data Source=SANDEEPTHAKUR\SANDY;database=Northwind;Integrated Security=SSPI") Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
•
•
•
•
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 Dim objConnection As New SqlConnection("Data Source=SANDEEPTHAKUR\SANDY;database=Northwind;Integrated Security=SSPI") Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub
enjoy!
![]() |
Similar Threads
- Custom page to login to Yahoo, Gmail, MSN etc (JavaScript / DHTML / AJAX)
- CDOSYS Form to Email Formatting (ASP)
- Email Form ? (HTML and CSS)
- PHP Problem (PHP)
- Is it usable, how to improve it.. please help (Website Reviews)
- got a newsletter sign up script? (Existing Scripts)
- Help with asp form will not display contact information (ASP)
- Simple Email Form (PHP)
- sessions help (PHP)
Other Threads in the ASP.NET Forum
- Previous Thread: UPDATE query problem
- Next Thread: Crystal Reports for ASP.Net
| Thread Tools | Search this Thread |
.net 2.0 3.5 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child click commonfunctions compatible confirmationcodegeneration content contenttype countryselector courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu edit expose feedback flash flv form formatdecimal forms formview gridview homeedition hosting iframe iis javascript jquery list listbox login menu microsoft mono mouse mssql multistepregistration nameisnotdeclared news numerical objects order panelmasterpagebuttoncontrols radio ratings rotatepage save schoolproject search security serializesmo.table silverlight smartcard sql-server sqlserver2005 suse textbox tracking typeof unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webarchitecture webdevelopemnt webservice xml youareanotmemberofthedebuggerusers






