title: Assign global variable from async method using c#
body: <p>I have an async method whereby i want to pull some data and assign to global variable for use in another method.
  Below is the method:</p>
  
  <pre><code>private async void getgoogleplususerdataSer(string access_token)
      {
          try
          {
              HttpClient client = new HttpClient();
              var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + access_token;
  
              client.CancelPendingRequests();
              HttpResponseMessage output = await client.GetAsync(urlProfile);
  
              if (output.IsSuccessStatusCode)
              {
                  string outputData = await output.Content.ReadAsStringAsync();
                  GoogleUserOutputData serStatus = JsonConvert.DeserializeObject&lt;GoogleUserOutputData&gt;(outputData);
  
                  if (serStatus != null)
                  {
                      gName = serStatus.name;
                      gEmail = serStatus.email;
                  }
              }
          }
          catch (Exception ex)
          {
              //catching the exception
          }
      }
  </code></pre>
  
  <p>The variables gName and gEmail have already been declared.
  I want to use the variables to register user using the following logic:</p>
  
  <pre><code> protected void RegisterGoogleUser()
      {
          string strCon = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
          string sql5 = "SELECT Email FROM Members where Email=@gEmail";
          using (SqlConnection con5 = new SqlConnection(strCon))
          {
              using (SqlCommand cmd5 = new SqlCommand(sql5, con5))
              {
                  con5.Open();
                  cmd5.Parameters.AddWithValue("@gEmail", gEmail);
                  Object result = cmd5.ExecuteScalar();
                  con5.Close();
                  if (result != null)
                  {
                      lblMessage.Text = "This e-mail is already registered. If you forgot your password, use forgot password link to recover it.";
                      return;
                  }
                  else
                  {
                      string providerName = "Google";
                      string conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
                      string query = "INSERT INTO Members(Name, Email, ProviderName)values(@gName,@gEmail,@providerName)";
                      using (SqlConnection connection = new SqlConnection(conn))
                      {
                          using (SqlCommand cmd1 = new SqlCommand(query, connection))
                          {
                              cmd1.Parameters.AddWithValue("@gName", gName);
                              cmd1.Parameters.AddWithValue("@gEmail", gEmail);
                              cmd1.Parameters.AddWithValue("@providerName", providerName);
                              connection.Open();
                              cmd1.ExecuteNonQuery();
                              Session.Add("GEmail", gEmail);
                              Session.Add("CurrentUserName", gName);
                              Response.Redirect("ExternalRegistration.aspx");
                              connection.Close();
                          }
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>Below is GoogleUserOutputData class and Google Login Method</p>
  
  <pre><code> public class GoogleUserOutputData
      {
          public string id { get; set; }
          public string name { get; set; }
          public string given_name { get; set; }
          public string email { get; set; }
          public string picture { get; set; }
      }
  protected void btnGoogleLogin_Click(object sender, System.EventArgs e)
      {
          var Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&amp;redirect_uri=" + googleplus_redirect_url + "&amp;scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&amp;client_id=" + googleplus_client_id;
          Session["Provider"] = "Google";
          Response.Redirect(Googleurl);
      }
  </code></pre>
  
  <p>The exception i am getting is: "The parameterized query '(@gEmail nvarchar(4000))SELECT Email FROM Members where Email=@g' expects the parameter '@gEmail', which was not supplied."</p>