title: How to get userID for username in ASMX webservice
body: <p>I'm trying to store a user's post into an access database through a web method. I want to store the logged-in user's username, user's post, and the post datetime. </p>
  
  <p>So far, I can store an existing user post by hard coding. But I want to store posts by any logged-in users. I was told I need to get userID for username.</p>
  
  <p>Thus, I've found and tried adding the following codes: </p>
  
  <pre><code>//GetUser() returns current user information
  MembershipUser user = Membership.GetUser();
  
  //Returns the UserID and converts to a string
  string UserID = user.ProviderUserKey.ToString();
  </code></pre>
  
  <p>When I tried debugging with breakpoints, the first one was okay. But for the second one, VS 2010 said that "object reference not set to an instance of an object." How do I fix it?</p>
  
  <p>VS suggested adding "new," which didn't work. It also suggested to catch NullReferenceException, but I don't know how to use the codes they provided:</p>
  
  <pre><code>public class EHClass
  {
      void ReadFile(int index)
      {
          // To run this code, substitute a valid path from your local machine
          string path = @"UsersDB_in_App_Data";
          System.IO.StreamReader file = new System.IO.StreamReader(path);
          char[] buffer = new char[10];
          try
          {
              file.ReadBlock(buffer, index, buffer.Length);
          }
          catch (System.IO.IOException e)
          {
              Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
          }
  
          finally
          {
              if (file != null)
              {
                  file.Close();
              }
          }
          // Do something with buffer...
      }
  
  }
  </code></pre>
  
  <p>Can you give me suggestions of what I need to do, or an alternative way to go about getting userID for username?</p>