0

I'm having an issue comparing 2 objects in an ASP.NET Project, I'm not sure if I am a beginner or what but this is my actual code:

private void CheckFriendshipStatus()
    {


        if (Object.ReferenceEquals(Session["UserId"].ToString(), Session["CurrentProfileId"].ToString()))
        {
            btnAddAsFriend.Visible = false;
        }
        else
        {
            DataTable dt1 = new DataTable();
            string chkfriendRequest = "SELECT * FROM Friends WHERE (MyId='" + Session["UserId"].ToString() + "' and FriendId='" + Session["CurrentProfileId"].ToString() + "') OR (MyId='" + Session["CurrentProfileId"].ToString() + "' and FriendId='" + Session["UserId"].ToString() + "')";

Then I tried:

    var obj1 = Session["UserId"].ToString();
    var obj2 = Session["CurrentProfileId"].ToString();


        if (obj1 == obj2)
        {
            btnAddAsFriend.Visible = false;
        }
    else
    {

and then I tried setting the value to null first and then assigning the session values. I'm out of ideas, any other one will be highly appreciated

2
  • What exactly is the issue? Is there an Exception? Does the comparison not return the expected result? Commented Dec 10, 2013 at 2:00
  • This is it: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Commented Dec 10, 2013 at 2:05

3 Answers 3

1

If you put strings into Session, you can get them back out as strings by casting.

var obj1 = (string)Session["UserId"]

If obj1 happens to be null, calling obj1.ToString() will cause a null reference exception. Casting as shown above will avoid that exception (though it may still be a logical error if one of them is null, depending on the conventions in your program).

If they are strings, you want to use the == operator. Object.ReferenceEquals() returns true if the two variables refer to the exact same object. Due to string interning, this would "coincidentally" commonly give you the expected result anyhow, but it is possible to turn off string interning.

UPDATE:

Based on your clarifying comment, it's almost certain that one or both of the Session variables you are calling .ToString() on is null. Use the casting approach that I outline, and use a debugger to see which is null.

Sign up to request clarification or add additional context in comments.

1 Comment

you know what, your answer seems to be pretty reasonable, however it does not work. but you know what I got it to work using the .ToString() way. incredible huh. it was actually that somehow Visual Studio was not working and I had to restart it, Voila! thank you though
0

Either Session["UserId"] or Session["CurrentProfileId"] or btnAddAsFriend is null. Can't tell which from the details provided.

1 Comment

Hi Spender, they are not set to null, they maybe were initialized in null but the value were added after it.
0

This Actually worked guys,

    var obj1 = Session["UserId"].ToString();
    var obj2 = Session["CurrentProfileId"].ToString();


    if (obj1 == obj2)
    {
       btnAddAsFriend.Visible = false;
    }

If this is happening to some of you that might be having the same code as step 1, this will resolve it!

Thank you all who took the time to waste it with me and help me! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.