When multiple people try to save a transaction the user id is going null to the database. I am fetching user information based on session id from database each time. I have tried query string and session also.
below mentioned is the code to retrieve logged user information from database on session id and system name.
I used to call these constructor is web method
public clsLoggedUser()
{
try
{
string strUserId = "";
if (HttpContext.Current.Request.QueryString["x"] == null)
{
string strSessionID = HttpContext.Current.Session.SessionID;
string strUrl = HttpContext.Current.Request.UrlReferrer.Authority;
string strSystemName = Convert.ToString(System.Net.Dns.Resolve(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]).HostName);
OracleParameter[] objParam = new OracleParameter[3];
objParam[0] = new OracleParameter("SESSIONID", OracleType.VarChar);
objParam[1] = new OracleParameter("SYSNAME", OracleType.VarChar);
objParam[2] = new OracleParameter("URL", OracleType.VarChar);
objParam[0].Value = strSessionID.Trim();
objParam[1].Value = strSystemName.Trim();
objParam[2].Value = strUrl.Trim();
DataTable dtUserDetails = clsOracleAccess.StaticExecuteDataset(CommandType.StoredProcedure, "PACKCOMMON.GETLOGGEDUSERID", objParam, true).Tables[0];
if (dtUserDetails.Rows.Count > 0)
{
strUserId = dtUserDetails.Rows[0]["APPLOCKID_NR"].ToString();
}
else
{
Exception ex = new NullReferenceException();
clsErrorLogEntity.fn_AddNewErrorLog(DateTime.Now, "clsLoggedUser.cs (clsLoggedUser(-1)) [" ex.Source "]:" ((String[])ex.StackTrace.Split(':'))[((String[])ex.StackTrace.Split(':')).Length - 1] "", ex.Message, ex);
}
}
else
{
strUserId = HttpContext.Current.Request.QueryString["x"].ToString().Trim();
}
if (!String.IsNullOrEmpty(strUserId))
{
OracleParameter[] objParam = new OracleParameter[1];
objParam[0] = new OracleParameter("USERID", OracleType.Number);
objParam[0].Value = Int32.Parse(strUserId);
DataTable dtUserDetails = clsOracleAccess.StaticExecuteDataset(CommandType.StoredProcedure, "HMPACK.GETUSERINFOBYID", objParam, true).Tables[0];
if (dtUserDetails.Rows.Count > 0)
{
UserID = Convert.ToInt32(dtUserDetails.Rows[0]["APPID_NR"].ToString());
UserName = dtUserDetails.Rows[0]["APPUNAME_CD"].ToString();
Password = dtUserDetails.Rows[0]["APPUACCESS_CD"].ToString();
UserCode = dtUserDetails.Rows[0]["APPCODE_CD"].ToString();
IsAdmin = Convert.ToChar(dtUserDetails.Rows[0]["ADMINUSER_YN"].ToString());
UserConID = Convert.ToInt32(dtUserDetails.Rows[0]["CONID_NR"].ToString());
IsLoggedIn = true;
blIsAdmin = IsAdmin == 'Y' ? true : false;
}
}
}
catch (Exception ex)
{
}
}
}
clsLoggedUser objclsLoggedUser = new clsLoggedUser();
objclsLoggedUser.UserName;
objclsLoggedUser.UserID;
CodePudding user response:
(sorry can't comment yet) I notice that there are quite a few possibilities to run trough that code and end up with a "objclsLoggedUser.UserName == null". If an exception occurs you might log using 'clsErrorLogEntity.fn_AddNewErrorLog' but 'if (!String.IsNullOrEmpty(strUserId))' has no else with a error logging. Your 'catch (Exception ex)' also doesn't log anything. If you set a break point at those locations and run the code, you are likely to find the culprit. Or after the 'catch (Exception ex)' set:
catch (Exception ex)
if(UserName == null)
throw new IllegalStateException($"User is NULL, HELP!! Some info: {HttpContext.Current.Session.SessionID}");
Set a breakpoint on the throw or watch the errors to get a clue what is going wrong.