The admin table is creating problem for me so I removed it....now check the solution I made:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Home : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
bool flag = true;
public Home()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
cmd = new SqlCommand();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogIn_Click(object sender, EventArgs e)
{
try
{
con.Open();
cmd.CommandText = "select * from [Users]";
cmd.Connection = con;
SqlDataReader rd = cmd.ExecuteReader();
if (txtUserName.Text == "admin" && txtPwd.Text == "admin")
{
Session["Username"] = txtUserName.Text;
Response.Redirect("Admin.aspx");
}
else
{
while (rd.Read())
{
if (rd["UserName"].ToString() == txtUserName.Text && rd["Password"].ToString() == txtPwd.Text)
{
Session["Username"] = rd["UserName"];
flag = false;
break;
}
}
if (flag == true)
lblMsg.Text = "Username and password invalid";
else
{
if(rd["Role"].ToString()=="Student")
Response.Redirect("Student.aspx");
else
Response.Redirect("Teacher.aspx");
}
}
}
catch(Exception ex)
{
lblMsg.Text = ex.Message;
}
}
}
It is working !!!!