Live Chat Icon For mobile
Live Chat Icon

How to create a login screen in ASP.NET

Platform: ASP.NET| Category: Basic

Here is a sample login screen:

<TABLE id='Table1' cellSpacing='1' cellPadding='1' width='300' border='1'>
<asp:Label id='Label1' runat='server'>User Name</asp:Label></TD>
<asp:TextBox id='txtUserName' runat='server'></asp:TextBox></TD>
<asp:RequiredFieldValidator id='RequiredFieldValidator1' runat='server' ErrorMessage='*' ControlToValidate='txtUserName'></asp:RequiredFieldValidator></TD>
<asp:Label id='Label2' runat='server'>Password</asp:Label></TD>
<asp:TextBox id='txtPassword' runat='server'></asp:TextBox></TD>
<asp:RequiredFieldValidator id='RequiredFieldValidator2' runat='server' ErrorMessage='*' ControlToValidate='txtPassword'></asp:RequiredFieldValidator></TD>
<asp:Button id='btnLogin' runat='server' Text='Login'></asp:Button></TD>


Dim myconnection As SqlConnection
Dim mycmd As SqlCommand
Dim strSql As String
Dim myReader As SqlDataReader

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        myconnection = New SqlConnection('Server=localhost;uid=sa;password=;database=northwind;')
        strSql = 'Select * from usertbl where username=' & '’' & txtUserName.Text & '’' & ' and userpassword=' & '’' & txtPassword.Text & '’'
        mycmd = New SqlCommand(strSql, myconnection)
        myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection)
        If myReader.Read() Then
            Response.Write('Access Denied')
        End If

End Sub


SqlConnection myconnection ;
SqlCommand mycmd ; 
string strSql ;
SqlDataReader myReader ;
private void btnLogin_Click(object sender, System.EventArgs e)
	myconnection = new SqlConnection('Server=localhost;uid=sa;password=;database=northwind;');
	strSql = 'Select * from usertbl where username=' + '’' + txtUserName.Text + '’' + ' and userpassword=' + '’' + txtPassword.Text + '’';
	mycmd = new SqlCommand(strSql, myconnection);
	myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
	if (myReader.Read() ) 
		Response.Write('Access Denied');

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.