Hi akhter,
Please refer below sample.
HTML
Default
<table>
<tr>
<td>UserName :</td>
<td><asp:TextBox runat="server" ID="txtUserName" /></td>
</tr>
<tr>
<td>Password :</td>
<td><asp:TextBox runat="server" ID="txtPassword" /></td>
</tr>
<tr>
<td><asp:Button ID="btnSend" Text="Send" runat="server" OnClick="OnSend" /></td>
</tr>
</table>
Home
<table>
<tr>
<td>UserId :</td>
<td><asp:Label runat="server" ID="lblUserId" /></td>
</tr>
</table>
Namespaces
Default
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Code
Default
protected void OnSend(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT UserId, Username, Password, RoleId From Users WHERE UserName = @UserName and Password = @Password", con))
{
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Session["UserName"] = sdr["Username"];
Session["UserId"] = sdr["UserId"];
Session["Password"] = sdr["Password"];
Session["RoleId"] = sdr["RoleId"];
if (Session["RoleId"].ToString() == "1")
{
Response.Redirect("Home.aspx");
}
else if (Session["RoleId"].ToString() == "2")
{
Response.Redirect("Home.aspx");
}
}
else
{
string Error = "UserName or Password is Incorrect";
ScriptManager.RegisterStartupScript(this,this.GetType(), "alert", "alert(' " + Error+ "' );", true);
}
con.Close();
}
}
}
Home
protected void Page_Load(object sender, EventArgs e)
{
lblUserId.Text = Session["UserId"].ToString();
}