save data from a DB in a Session Varible and how to use it in a second Form

fernando salaburu 21 Reputation points
2021-07-04T14:57:24.88+00:00

in a table I have some permissions that the user has and I find it this way

    private void permisos()
    { 
    string UI = ConfigurationManager.AppSettings["User Id"];
    string PS = ConfigurationManager.AppSettings["Password"];
    string DS = ConfigurationManager.AppSettings["Data Source"];

    OracleConnection m = new OracleConnection();
    m = new OracleConnection("Data Source=" + DS + "; User Id=" + UI + "; Password=" + PS + ";");
    m.Open();
        //para buscar la info en una tabla

        OracleDataAdapter conecto3 = new OracleDataAdapter("select D_NO from empresa.web_access where  TRIM(U_NO) = TRIM('" + TxtUsuario.Text + "')", m);
        DataSet access = new DataSet();
        conecto3.Fill(access);

        m.Close();
     

my idea is to save it in a variable Session ["AC"]
this can be ???

and the second form, how do I call this session variable and look for the information within it, to read and find the user's permissions

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Albert Kallal 5,591 Reputation points
    2021-07-04T20:05:17.95+00:00

    Ok, you probably should setup some global function to get that connection stuff.

    and use a datatable in place of a dataset (a dataset is a collection of tables - you only have and need one)..

    But, you can do this:

    Session["MyAccess"] = access;
    

    Now, on any other web page, you can do this:

    Datatable access = Session["MyAccess']
    

    At this point then, any value in that table can be had like this:

     string strTest = access.Rows[0]["some column name here]"
    

    So, once you shove the table into session[], then you can pull that table out back into a datatable anytime you want from that point on - it will persist for life time of that logged on user session.

    Note that I have in some cases pulled out a row, and thus don't even have to use the rows collection, since you (we assume) ever only have one row. However, for some strange reason, a data row cannot be serialized, and if you use sql server (or I suppose oracle) based session management, then it uses serialization.

    But, no worries a dataset, (or better datatable) can be serialized , and thus you should have no issues shoving (saving) that table into session.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada


1 additional answer

Sort by: Most helpful
  1. fernando salaburu 21 Reputation points
    2021-07-08T20:08:27.297+00:00

    I could do it there and it keeps well, thanks for everything!

    what changes

            DataTable access = new DataTable();
    
            //DataSet access = new DataSet(); before!!!
    

    so I remain


        private void permisos()
        { 
        string UI = ConfigurationManager.AppSettings["User Id"];
        string PS = ConfigurationManager.AppSettings["Password"];
        string DS = ConfigurationManager.AppSettings["Data Source"];
    
        OracleConnection m = new OracleConnection();
        m = new OracleConnection("Data Source=" + DS + "; User Id=" + UI + "; Password=" + PS + ";");
        m.Open();
            //para buscar la info en una tabla
    
            OracleDataAdapter conecto3 = new OracleDataAdapter("select D_NO from empresa.web_access where  TRIM(U_NO) = TRIM('" + TxtUsuario.Text + "')", m);
            DataTable access = new DataTable();
            //DataSet access = new DataSet(); antes!!!
            conecto3.Fill(access);
            Session["MyAccess"] = access;
    

    in the other form and I go through it


    DataTable access = (DataTable)Session["MyAccess"];
    //esto recorre la tabla (Datatable)
    for (int i = 0; i < access.Rows.Count; i++)
    {
    string door = access.Rows[i]["D_NO"].ToString();

                if (door == "2050")
                {
                    break;
    
                }
    

    }

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.