Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Object Convert

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2.0] Object Convert

    Hi all
    How to convert any value as class object.
    I am getting error

    Cannot implicitly convert type 'object' to 'UserTracking'. An explicit conversion exists (are you missing a cast?)

    C# Code:
    1. void Session_Start(object sender, EventArgs e)
    2.     {
    3.         // Code that runs when a new session is started
    4.         try
    5.          {
    6.              UserTracking userTracking = new UserTracking();
    7.              Session["UserTracking"] = userTracking;
    8.              Response.AppendToLog("SessionStart");
    9.          }
    10.          catch
    11.          {
    12.              //Expection
    13.          }
    14.     }
    15.     void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    16.     {
    17.         if (Context.Session != null)
    18.         {
    19.             UserTracking userTracking = new UserTracking();
    20.             userTracking =Session["UserTracking"]; //Error Getting Here :wave:
    21.             if (userTracking != null)
    22.             {
    23.                 //userTracking.AddPage(Request.Url.ToString());
    24.             }
    25.         }
    26.        
    27.     }

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2.0] Object Convert

    You have to cast it to a UserTracking object like so:

    C# Code:
    1. userTracking = (UserTracking)Session["UserTracking"]; //Error Getting Here :wave:
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Object Convert

    The 'userTracking' variable is type UserTracking. The indexer of the Session property returns an Object reference. You cannot assign a reference of type Object to a variable of type UserTracking. You know that. If the object returned by the indexer of the Session property IS type UserTracking then you have to cast it as that type:
    Code:
    userTracking = (UserTracking)Session["UserTracking"];
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: [2.0] Object Convert

    Thanks you both for quick help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width