Hi friends,
I want to make a login form having details id and password and whatever we enter inside the login details then how we will match that details with database then if it correct then gain access otherwise login failed. Please give me an explanation with an example.
Login form in ASP.NET
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.


Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 30, 2011, 8:20 AM EST
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Button1_Click(object sender, EventArgs e)
{
object obj = null;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
string str = "select count(*) from login where UserName=@UserName and Password =@Password";
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserName", TextBox_user_name.Text);
cmd.Parameters.AddWithValue("@Password", TextBox_password.Text);
obj = cmd.ExecuteScalar();
if ((int)(obj) != 0)
{
lb1.Text = "WELLCOME :: " + TextBox_user_name.Text;
}
else
{
lb1.Text = "Invalid user name and password";
}
con.Close();
}
}
Thanks
If this post helps you mark it as answer