Monday, October 8, 2007

How to create a login form

Here we will discuss how to create a login form in Asp.net 2.0 using C#
Sql Server 2000

create a database table in sql server with following fields

user_name varchar(50) Not Null
password varchar(50) Not Null

and add one or two values in table thru Enterprise Manager

Or you can use query analyzer to create tables

Add the following code in login.aspx.cs file

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_Login : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
con.Open();
}
// check authorization for registered admin
protected void Button1_Click(object sender, EventArgs e)
{
string a = uname.Text;
Session["ds"] = a; //here we have created session for admin
if (uname.Text == "")
{
Label1.Text = "User Name Cannot be null";
Label1.Visible = true;
}
if (password.Text == "")
{
Label1.Text = "Password Cannot be null";
Label1.Visible = true;
}
string u =Convert.ToString (uname.Text);
string p =Convert.ToString( password.Text);
//Response.Write(u);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select user_name, password from Pefa_Admin ";
SqlDataReader dr = cmd.ExecuteReader();
////cmd.ExecuteReader = dr;
while (dr.Read())
{
string username =Convert.ToString( dr[0]);
string pass =Convert.ToString( dr[1]);
if (u == username && p == pass)
{
Response.Redirect("Wait.aspx");
}
else
{
Response.Write("Not Authprised");
}
}
}
}


And add a web.config file to create a connection













1 comment:

Anonymous said...

Ist mit Ihnen Einverstanden levitra preis cialis rezeptfrei [url=http//t7-isis.org]levitra vs viagra dosage[/url]