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'>
<TR>
<TD>
<asp:Label id='Label1' runat='server'>User Name</asp:Label></TD>
<TD>
<asp:TextBox id='txtUserName' runat='server'></asp:TextBox></TD>
<TD>
<asp:RequiredFieldValidator id='RequiredFieldValidator1' runat='server' ErrorMessage='*' ControlToValidate='txtUserName'></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD>
<asp:Label id='Label2' runat='server'>Password</asp:Label></TD>
<TD>
<asp:TextBox id='txtPassword' runat='server'></asp:TextBox></TD>
<TD>
<asp:RequiredFieldValidator id='RequiredFieldValidator2' runat='server' ErrorMessage='*' ControlToValidate='txtPassword'></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD></TD>
<TD>
<asp:Button id='btnLogin' runat='server' Text='Login'></asp:Button></TD>
<TD></TD>
</TR>
</TABLE>

VB.NET


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)
        myconnection.Open()
        myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection)
        If myReader.Read() Then
            Response.Write('Welcome')
        Else
            Response.Write('Access Denied')
        End If

End Sub

C#


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);
	myconnection.Open();
	myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
	if (myReader.Read() ) 
	{
		Response.Write('Welcome');
	}
	else
	{
		Response.Write('Access Denied');
	}
}

Share with

Share on twitter
Share on facebook
Share on linkedin