The things I learnt while I migrated from classic asp to is given here for my reference as well as for new beginners.

Login Form - Code in ASP.NET Csharp

Today what we are going to Learn?



What Language are we going to use?

Let us use C# with code-behind.

How many files will be there in this program?

The program contains two files: login.aspx and login.aspx.cs. In the Login.aspx the form will be there and the csharp code will be in login.aspx.cs.

What about Database?

We will create myDatabase.mdb in MS Access. Inside this mdb we will create a table with name "LOGIN". And insert the data as shown here:

How the output of our Login Form will look Like?

Have a look at this Screenshot:



Can you first show me the code? Code for Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" ></asp:Label>
<br />
User Name: <asp:TextBox ID="UserId" runat="server" /><br />
Password: <asp:TextBox ID="Pwd" runat="server" /><br />
<asp:Button ID="Login" runat="server" Text="Login" onclick="Login_Click" />

Code for Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
public partial class login : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void Login_Click(object sender, EventArgs e)
string sConn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("myDatabase.mdb");
string sCmd = "Select Count(*) From login Where UserId = ? and Pwd= ?";
int cnt = 0;
OleDbConnection myConn = new OleDbConnection(sConn);
OleDbCommand myCmd = new OleDbCommand(sCmd, myConn);
myCmd.Parameters.AddWithValue("", UserId.Text);
myCmd.Parameters.AddWithValue("", Pwd.Text);
cnt = (int)myCmd.ExecuteScalar();
if (cnt == 0) { Label1.Text = "Invalid UserId or Password"; }
else { Response.Redirect("Welcome.aspx"); }

I have installed Visual Web Developer 2008. But I dont know how to start to create this page. Can you explain, in a step-by-step way?

Step 1: Open the Web Developer. The screen will look like this:

Click on the Create Web Site to create a NEW website. You will get the screen like this:

Have A look at screenshot. I have selected website=ASP.NET, Language=C# and selected a folder.The folder name was by default showing it as "website" but I have changed it into "TUTORIAL". and when I pressed OK, I got the screen like this:


Now, by default, Default.aspx has been created. But I dont want this file. I could have renamed this file into Login.aspx. But I am not going to rename it. Let us create a new file with name Login.aspx. The Steps are:

Step1: In the solution explorer, right click on the Tutorial web site. You will get a context Menu. From the Content Menu, Select "ADD NEW ITEM". See the shot:

Step 2: In the Add New Item, I have selected WEBFORM,I have typed my file name as Login.aspx, I have selcted C# as my language and I have selected "PLACE CODE IN SEPARATE FILE" which means, code-behind-method.

Then, From the Toolbox, I dragged and dropped 1 Label,2 TextBox and 1 Button into the <div> tag.


Save this file and right click on the "login.aspx" in the Solution Explorer and select "VIEW IN BROWSER". You should get the output like this:

Next, copy the code of login.aspx.cs I have given above and paste it into YOUR login.aspx.cs.

But how to create login.aspx.cs file?

When you create login.aspx, this login.aspx.cs is also created automatically. Are you able to see a plus sign on the left side of login.aspx in solution explorer. Click on this plus sign and you will see the cs file. Note: cs stands for csharp.

I am not able to see the SOLUTION EXPLORER OR TOOL BOX.Why?


If the SOLUTION EXLORER or TOOLBOX is not visible, then open the view View Menu and see there.


Thanks for Your Visit

Google Search
Disclaimer and Copy Right