Tuesday, May 25, 2010

Login Page Using C# .Net

SQL Server
==========

Create Table Login
(
UName Varchar(8),
Password Varchar(5)
)

select * from Login

Insert Into Login Values('sudha','12345')

Select UName,Password from Login where UName='sudha' and Password='12345'

=================================================================================


C# Code
=======

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

public partial class _Default : System.Web.UI.Page
{
SqlConnection Conn = new SqlConnection("Data Source=GNANASUDHAHAR\\SQLEXPRESS;Initial Catalog=sudha;Integrated Security=true");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
Conn.Open();
SqlCommand Cmd = new SqlCommand("Select UName,Password from Login where UName='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'", Conn);
SqlDataReader DR;
DR = Cmd.ExecuteReader();
if (DR.Read())
{
Response.Redirect("home.aspx");
}
else
{
Response.Write("Invalid");
}
Conn.Close();
}
catch
{
Response.Write("Error");
}
}
}