user login code
A user login when successfull login then will shown his profile on next page from sql database in c# asp.net
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Dec 8, 2013, 2:10 PM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Login_related_details.Login" %>
Login.aspx.cs
using System;
using System.Collections;
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;
namespace Login_related_details
{
public partial class Login : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
string str;
DataTable dt;
int RowCount;
protected void btn_login_Click(object sender, EventArgs e)
{
string UserName = TextBox_user_name.Text.Trim();
string Password = TextBox_password.Text.Trim();
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "Select * from Login";
com = new SqlCommand(str);
sqlda = new SqlDataAdapter(com.CommandText, con);
dt = new DataTable();
sqlda.Fill(dt);
RowCount = dt.Rows.Count;
for (int i = 0; i < RowCount; i++)
{
UserName = dt.Rows[i]["UserName"].ToString();
Password = dt.Rows[i]["Password"].ToString();
if (UserName == TextBox_user_name.Text && Password == TextBox_password.Text)
{
Session["UserName"] = UserName;
Response.Redirect("Details.aspx");
}
else
{
lb1.Text = "Invalid User Name or Password! Please try again!";
}
}
}
}
}
Details.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Details.aspx.cs" Inherits="Login_related_details.Details" %>
Details.aspx.cs
using System;
using System.Collections;
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;
namespace Login_related_details
{
public partial class Details : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
lb1.Text = "" + "WELLCOME:: " + "" + "" + Session["UserName"] + "";
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from Login where UserName='" + Session["UserName"] + "'";
com = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
lbl_UserName.Text = ds.Tables[0].Rows[0]["UserName"].ToString();
lbl_address.Text = ds.Tables[0].Rows[0]["address"].ToString();
lbl_sal.Text = ds.Tables[0].Rows[0]["sal"].ToString();
lbl_phone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
}
}
}