Cannot retrieve Session value - asp.net

I've written a simple code to keep a USerID with the help of session. However I can't get Session value although it's not null. I've done exactly the way microsoft official tutorial says. Here's the code:
The code on the Default.aspx
string regCode = loginBase.getRegCodePerUser(txtLogin.Text);
Session["regCode"] = regCode;
//lblInfo.text=(string)Session["regCode"];When I check it shows the right string.It's OK
Response.Redirect("Selection.aspx");
I do not directly go to that page.I first go to Selection.aspx, then UpdateStages.
And this is the code on the other page(UpdateStages.apsx):
if ((string)Session["connSTR"] == null && (string)Session["user"] == null)
{
Response.Redirect("Default.aspx");
}
else if ((string)Session["regCode"]!=null)
{
regCode=(string)Session["regCode"];
lblInfo.Text = regCode;//Show nothing. Empty.
}

It might very well be that when you set the session variable:
Session["regCode"] = loginBase.getRegCodePerUser(txtLogin.Text);
That the username is not available yet and that is why it's returning an empty string, and I would assume that you are executing this code: lblStatus.Text =loginBase.getRegCodePerUser(txtLogin.Text); on a different page/after the user has logged in and that is why you get the value in the label when you assign it directly.
Put a breakpoint on where you set the Session variable and see if the value is being set.
Otherwise your session settings could be incorrect in your web.config which causes the session values to be cleared before you get to your second page where you are accessing it.

Related

How to remove html link from master page load function using session variable in ASP.Net?

I want to remove the links if the session variable is having value available in page load function.
if (Session["UserLogin"].ToString().Equals("available"))
{
HtmlAnchor linkCreateUser = (HtmlAnchor)this.Master.FindControl("linkCreateUsers");
linkCreateUser.Visible = false;
}
Admin.Master
Create User
I tried the above code. But it throws an error
Object reference not set to an instance of an object.
I created this session variable when user logged in on the login page.
Your code hides the link if you just run it. Your code propbably craches when the Session itself is null.
So check for that also.
if (Session["UserLogin"] != null && Session["UserLogin"].ToString().Equals("available"))

The session state Information is invalid and might be corrupted in ASP.Net

I am using ASP.Net 3.5 with C#,Development ID:Visual Studio 2008. When I am using
Session["FileName1"] = "text1.txt"
it is working fine, but then I am using
number1=17;
string FileName1="FileName1" + number1.toString();
then setting with
Session[FileName1]="text1.txt";
gives me runtime error
The session state information is invalid and might be corrupted
at System.Web.SessionState.SessionStateItemCollection.Deserializer(BinaryReader reader)
Can anybody solve my problem, when I am using string in the Session variable? Remember it works on my development machine (meaning local Visual Studio) but when deployed to the server it gives mentioned error.
Make sure the FileName1 variable is not null before trying to access it via the Session[FileName1] syntax...
Here's a link to someone else that was having the same problem:
http://forums.asp.net/t/1069600.aspx
Here's his answer:
In the code, I found the following line:
//some code
Session.Add(sessionVarName, sessionVarValue);
//some other code
Apparently, because of some dirty data, there is a time when
sessionVarName is null.
Session.Add will not throw any exception in this case, and if your
Session Mode is "InProc", there will be no problem. However, if your
Session Mode is "SQLServer", during deserialization of the session
store, you will got the exception that I got. So, to filter out dirty
data, I modified the code to become:
if (sessionVarName != null)
{
//somecode
Session.Add(sessionVarName, sessionVarValue);
//some other code
}
the reason of your error is
xyz = new Guid() is also xyz= Guid.Empty;
so when you try to convert to string it's throw error.
just modify you code something like that.
Guid guId = System.Guid.NewGuid();
string x = guId .ToString();
string FileName1="text1.txt" + x;
Session[FileName1]="text1.txt";
Check your values before storing them in session they may cause this exception during deserialization of the session store, Filter your data .
Check Here
if(!string.IsNullOrEmpty(FileName1))
{
Session.Add(FileName1, "text1.txt");
}
Or check for Invalid characters in your string .
You can add the Value into session Like this
string FileName1="FileName1" + number1.toString();
if(!string.IsNullOrEmpty(FileName1))
{
Session.Add(FileName1, "text1.txt");
}

Do cookies have the equivalent of a primary key?

Coming from a non-web background I'm struggling with cookie uniqueness. When I read and write to a cookie named CustomerCode I find multiple cookies with the same name in my cookie collection(s). How can this be avoided?
Database rows use a primary key to ensure uniqueness. Is there an equivalent for cookies? I'm using this "Reusable Cookie Container" code to simplify writing to a cookie:
Master.Cookies.CustomerCode = SessionWrapper.CustomerCode;
Then in my SessionWrapper I restore session variables from the cookie(s)
public static void InitiateSessionVariablesFromCookies(IAppCookies appCookies) {
if (SessionWrapper.CustomerCode == null && appCookies.CustomerCode != null) {
SessionWrapper.CustomerCode = appCookies.CustomerCode;
}...
The cookie collection contains CustomerCode multiple times so the wrong value is being passed to the session variable. If this question is difficult to answer without seeing all of my code please describe the proper / best way to set cookies and then read them back into session variables (or include a link to help me out).
Thanks in advance.
If you have different expiration date/times you can get "duplicates".
HttpCookie Temp = new HttpCookie("MyName", "123");
Temp.Expires = DateTime.Now.AddMinutes(5);
Response.Cookies.Add(Temp);
This code will create a new cookie each time it runs with the same name and value.

Do action if button clicked for 3 times

I want to do some action if button clicked for 3 times. Just like if users enters a wrong password for 3 times the page the page must be redirected to another page or something.
How to do action in ASP.NET C# if button clicked for 3rd time?
To check if a user has taken an action fox "x" times you need to store it somehwere. If you're using a relation database you could for example call a table LoginAttempts and there you store all unsuccesfull logins. When you have that table you can build your logic against it.
I'd use a session variable.
Another solution could be having a column in the users table named LoginAttempt(int)(default 0), how I would use that column is
Let's say you have a table in your databese called TblUsers with these columns
Id,
UserName,
Password,
LoginAttempt.
And let's say you have two TextBoxes on your Login.aspx page
TextBoxUserName,
TextBoxPassword.
Let's say you have one record in your TblUsers like this
Id : 1
UserName : rammstein
Password : ohnedich
LoginAttempt : 0
Now, you are in your Login.aspx.cs code
you have a method and in it you have
TblUsers user = new TblUsers();
And you have a bool login = false;.
You've got the username from TextBoxUserName.Text, you check if a user with this username exists then if it exist you do the below code.
Let's follow this scenerio
The given user tried to login with
UserName:rammstein
Password:duhast
Checked your database with username rammstein and found it, and took that record in your TblpUsers user then you checked whether user.Password and TextBoxPassword.Text matches.Well it won't match for the above example because
user.Password is ohnedich however TextBox.Password is duhast.This means the login is not successfull so you set false to your bool login.
Everything else belongs to the below code with if-else condition
if(user.LoginAttempt < 3){
if(!login)
{
user.LoginAttempt = user.LoginAttempt + 1;
}
else
{
user.LoginAttempt = 0;
}
}
else
{
//do something
}
Multiple solutions:
1) Session variable (Okay solution)
2) Static class variable (Not a good solution)
3) DB record field (Best solution)
4) Pass flag variable back and forth between view and controller (not a very good idea).
5) Browser cookie (can be cleared)

Problems while trying to retrieve value from arraylist stored in session

Please help me. Here is my code
ArrayList arrValues = new ArrayList();
arrValues = (ArrayList)Session["ArrValues"];
string CustName, Addr1, Addr2, City, State, Country, Pin, Email,Order,CustToken;
string SName, SAddr1, SAddr2, SCity, SState,SPin, SCountry;
CustName = arrValues[1].ToString().Trim();
It is thrwoing a "NullReference excption" while trying to get the value of CustName from the arraylist stored in the session. Below is the link to see the video
http://www.talash.com/testingvideo/2011-03-18_0958_Payment_Gateway_Problem.swf
The problem is that your session variable doesn't exist.
arrValues is null. You need to look into why you are loosing your session variable.
Are you trying to get this value from within a page or a module?
If you work with HttpModule the session is not available at all times and it might be that the session object is null.
since your session variable is not set to any value previously thats why u are getting an error.make this code safe by putting it inse something like this..
if(Session[Arrvalues"]!=null)
{
//your block of code//
}
else
{
Response.Redirect("to_the_page_where_this_session_variable_is_set.aspx");
}

Resources