locked
Need help with creating a global variable RRS feed

  • Question

  • User606090160 posted

    Hello i am trying to create a class that i need to access a variable via sql query.  Not sure if i set up the class properly because i cant seem to get the variable to string.  This is what i tried to put together based off my notes and limited knowledge of creating classes.

     public class district
        {
            public string districtID { get; set; }
        }
    
        public static class DistrictID
        {
            static district myDistrict;
    
            public static district MyID
            {
                get
                {
                    if (myDistrict == null)
                    {
                        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyMatrixConnectionString"].ConnectionString))
                        {
                            var username = Membership.GetUser();
                            conn.Open();
                            string sqlquery = @"SELECT districtID FROM User_Info WHERE username = @username";
                            SqlCommand cmd = new SqlCommand(sqlquery, conn);
                            cmd.Parameters.Add(new SqlParameter("@userName", username.ToString()));
                            SqlDataReader reader = cmd.ExecuteReader();
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    district d = new district();
                                    d.districtID = reader["districtID"].ToString();
                                    myDistrict = d;
                                }
    
                            }
                        }
    
    
                    }
                    return myDistrict;
                }
    
            }
        }

    usage (not working)???

    string districtID = DistrictID.MyID.districtID;

    Monday, March 20, 2017 8:22 PM

All replies

  • User475983607 posted

    The design will not work because a static member variable is a single memory location for all users of the web site.  The first user to set the districtId will apply to all users.

    A simpler way to do this is create a cookie that has the districtId when the user logs in for the first time.  Or add the districtId to the user data property of the membership authentication cookie.

    Monday, March 20, 2017 8:56 PM
  • User606090160 posted

    Thats what i did originally but i noticed the session cookie expiring and what not.  I thought i offer a more reliable solution.  

    Tuesday, March 21, 2017 2:00 AM
  • User475983607 posted

    Thats what i did originally but i noticed the session cookie expiring and what not.  

    I did not suggest using Session.  I suggested using a cookie or adding the distinctId to the userdata property of the forms authentication ticket (Membership).

    https://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.userdata(v=vs.110).aspx

    I thought i offer a more reliable solution.  

    You solution will not work as you expect.  The first user to set districtId will set this value for every single user of your application until the application restarts.

    Tuesday, March 21, 2017 10:30 AM
  • User765422875 posted

    Read the following on creating a global variable.

    https://www.dotnetperls.com/global-variables-aspnet

    It seems like you just want to store an ID. There is no need to have a class just for one field. In your Global class, just create a DistrictID property.

    Also, just call ExecuteScalar() on the command to get one value from a query.

    Example

                    string districtId = string.Empty;
                    using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyMatrixConnectionString"].ConnectionString))
                    {
                       var username = Membership.GetUser();
                       conn.Open();
                       string sqlquery = @"SELECT districtID FROM User_Info WHERE username = @username";
                       SqlCommand cmd = new SqlCommand(sqlquery, conn);
                       cmd.Parameters.Add(new SqlParameter("@userName", username.ToString()));
                       districtId = (string) cmd.ExecuteScalar();                  
                    }

     

    Thursday, March 30, 2017 7:56 PM