laurence keith albano
laurence keith albano

Reputation: 1482

How to get a specific data from your database using Session?

I want to get a specific data from my database when login. The default login username for my web page is a student ID. Example (studentid="2011018921") this is the username that I declare when logging in, but I want to get a specific data in my database when I login to the system, like I want to get the firstname of the student.

this code here gets the data of the username.

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 MSSQLConnector;
using System.Data;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class OnlineAppSyss : System.Web.UI.Page
    {
        private MSConnector connector = new MSConnector();
        string query = null;
        string clear = "";
        int rowcounter = 0;
        private DataSet selectedData;

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            connector.ConnectionString = "Data Source=keith;Initial Catalog=Student;Integrated Security=True";

            string username = (this.UserName.Value);
            string pass = (this.Password.Value);

            query = "select studentid,password from student";

            selectedData = connector.ExecuteQuery(query);


            for (;;)
            {
                string userid = selectedData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
                string password = selectedData.Tables[0].Rows[rowcounter]["Password"].ToString();
                //Username and Password are correct
                if ((username == userid) && (pass == password))
                {
                    //This session Login I want to get the firstname of the student
                    Session["login"] = userid;

                    Response.Redirect("StudentPage.aspx", true);
                    break;
                }
                //admin side, correct username and password
                else if ((username == "admin") && (pass == "cmpe1234"))
                {
                    Response.Redirect("AdministratorPage.aspx");
                }
                //admin side, correct username invalid password
                else if ((username == "admin") && (pass != "cmpe1234"))
                {
                    Response.Write("<script language=javascript>alert('Username and password does not match. Try again1');</script>");
                    break;
                }
                //empty username and password
                else if (((username == "") && (pass == "")) || ((userid == null) && (password == null)))
                {
                    Response.Write("<script language=javascript>alert('Username and password must have input value. Try again2');</script>");
                    break;
                }
                //username is invalid and password is correct ; username is correct and password is invalid
                else if (((username != userid) && (pass == password)) || ((username == userid) && (pass != password)))
                {
                    Response.Write("<script language=javascript>alert('Username and password does not match. Try again');</script>");
                    break;
                }
                else
                {
                    rowcounter++;
                }
            }
        }
    }
}

this is where I check my username in another aspx code behind to see the studentid log.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class StudentPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["login"]==null)
            {

            }
            else
            {
                //it states here (Welcome 2011018921)
                labelsession.Text = "Welcome  " + Session["login"].ToString();
            }
        }
    }
}

How do I change my code to state (Welcome Keith) instead of a studentID declare it to FirstName.

I guess this is something to do with this code here?

        string userid = selectedData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
        string password = selectedData.Tables[0].Rows[rowcounter]["Password"].ToString();
        //Username and Password are correct
        if ((username == userid) && (pass == password))
        {
            Session["login"] = userid;

            Response.Redirect("StudentPage.aspx", true);
            Response.Write("<script language=javascript>alert('Login Successful');</script>");
            break;
        }

How do I declare the Session to display the firstname of my data without changing the username authentication as (studentID)?

Upvotes: 0

Views: 218

Answers (1)

Shaminder Singh
Shaminder Singh

Reputation: 1293

Well its not a good way to authenticate, but as per your requirement, modify your code as: change your query to

query = "select firstname,studentid,password from student";

Then modify your for loop by replacing the upper code as :

            string userid = selectedData.Tables[0].Rows[rowcounter]["StudentID"].ToString();
            string password = selectedData.Tables[0].Rows[rowcounter]["Password"].ToString();
            string firstname = selectedData.Tables[0].Rows[rowcounter]["FirstName"].ToString();
            if ((username == userid) && (pass == password))
            {
                //This session Login I want to get the firstname of the student
                Session["login"] = userid;
                Session["firstname"] = firstname ;
                Response.Redirect("StudentPage.aspx", true);
                break;
            }

use firstname anywhere you want it

Upvotes: 1

Related Questions