Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
3.33/5 (2 votes)
i am doing a project(website) in asp.net c#. I am using visual studio 2005 and SQL server 2005.What I need to know is when a student logs in using their username and password they should have their specific information displayed from the database (like name,dob,class,department, contactno from the SQL) on to a different web page. i am able to display the data for only one user.So how can i display different users data when different user logs in with his username and password.

I am using two pages one for login and other studentinf which displays the information of current user login.

code for login page:

SQL
public partial class _Default : System.Web.UI.Page 
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
SqlDataAdapter da; 

protected void Page_Load(object sender, EventArgs e)
{
    con = new SqlConnection("Data Source=localhost;Initial Catalog=knm;Integrated Security=SSPI");
    con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
    bool temp = false;
    String qry=" Select * from user_login where username='" + TextBox1.Text+"' and password='" +TextBox2.Text+"'";
    cmd=new SqlCommand(qry,con);      
    dr=cmd.ExecuteReader();
    if(dr.Read())
    {
        Label3.Text="Logged in successfull";
        Response.Redirect("studenthome.aspx");
    }

        else
        Label3.Text="invalid user";
    dr.Close(); 
    }

}


code for studentinf page:

SQL
public partial class _Default : System.Web.UI.Page                                                                           
{
   SqlConnection con;
   SqlCommand cmd;
   SqlDataReader dr;
   SqlDataAdapter da;

protected void Page_Load(object sender, EventArgs e)
{
    bool temp = false;
    con = new SqlConnection("Data Source=localhost;Initial Catalog=knm;Integrated Security=SSPI");
    String std = "SELECT* FROM user_login";
    con.Open();
    cmd = new SqlCommand("select * from studentinf ", con);      
    da = new SqlDataAdapter("select * from studentinf ", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "studentinf");
    TextBox1.Text = ds.Tables["studentinf"].Rows[0]["name"].ToString();
    TextBox2.Text = ds.Tables["studentinf"].Rows[0]["dob"].ToString();
    TextBox3.Text = ds.Tables["studentinf"].Rows[0]["class"].ToString();
    TextBox4.Text = ds.Tables["studentinf"].Rows[0]["department"].ToString();
    TextBox5.Text = ds.Tables["studentinf"].Rows[0]["contact"].ToString();  
     }
} 
Posted

1 solution

See my reply to this[^] question.  Use the same technique to solve your problem.

/ravi
 
Share this answer
 
Comments
Member 10591727 2-Mar-14 14:59pm    
could u pls tell me how to use a where clause so that only the login users data is displayed
Ravi Bhavnani 2-Mar-14 16:33pm    
See this link: http://www.w3schools.com/sql/sql_where.asp

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900