Link to home
Create AccountLog in
Avatar of fwsteal
fwsteal

asked on

query results into session variable

how do I put query results into session variable?

protected void btnLogin_Click(object sender, EventArgs e)
    {
        String strUserId = txtbxSiteCode.Text.ToString();
        String strPassword = txtbxPassword.Text.ToString();

        if (ValidateUser(strUserId, strPassword))
        {
            //generate a session variable
            SqlCommand cmd = new SqlCommand("uspGetCIDFKBySiteCode", new SqlConnection(myConn));
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@SiteCode", strUserId);
            cmd.Connection.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            cmd.Connection.Close();
            cmd.Connection.Dispose();
            //something like?
            //Session["CIDFK"] = Convert.ToInt32(CIDFK.fieldvalue);
            //how do I put the value into a session like this?  Session["CIDFK"] = 30; where the cidfk is an interger
            FormsAuthentication.RedirectFromLoginPage(strUserId, false);
            Response.Redirect("audit/default.aspx");
            //I read that SqlDataReader would do what you want it to and work great for single table returns.
            //You can store them to session variables and access the elements.
            //how is this achieved?
        }
        else
        {
            Response.Redirect("login.aspx", true);
        }
    }

I'm just not sure how to do it.
Avatar of KarinLoos
KarinLoos

Session["CIDFK"] = CIDFK.fieldvalue.ToString();

Avatar of fwsteal

ASKER

how do I determine if the session variable is empty or null?

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["CIDFK"] == "")
        {
            Response.Redirect("../default.aspx", true);
        }

        //set the title of the page
        this.Title = SiteConfiguration.SiteName;
        String mystring = Session["CIDFK"].ToString();
        Response.Write(mystring);
ASKER CERTIFIED SOLUTION
Avatar of KarinLoos
KarinLoos

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of fwsteal

ASKER

had to do this to get it to work:

        string CIDFK = null;
        /// check for existance of the session variable
        if (Session["CIDFK"] != null)
        {
            CIDFK = Session["CIDFK"].ToString();
        }
        if (CIDFK == null)
        {
            Response.Redirect("../default.aspx", true);
        }