title: C# winforms save UserID value to class
body: <p>I'm trying to save UserID value to a class after an successful login. When the login button is clicked, it validates the login into in text boxes using the  @user, @pass.</p>
  
  <p>My problem is that I do not know how to write a sql reader and save the resulting int to a class. I also do not know how to write that class.</p>
  
  <p>I need to save it to a class so I can use it on different forms to check what account the user is logged into. 
   EDIT: Updated code from suggestions but i get errors <a href="https://imgur.com/jOZ24DP" rel="nofollow noreferrer">Error picture</a> any ideas where i did a mistake?</p>
  
  <pre><code>private LoginUser validate_login(string user, string pass)
      {
          db_connection();
          MySqlCommand cmd = new MySqlCommand();
          cmd.CommandText = "Select * from table2 where username=@user and password=@pass";
          cmd.Parameters.AddWithValue("@user", user);
          cmd.Parameters.AddWithValue("@pass", pass);
  
  
          cmd.Connection = connect;
      LoginUser usr = new LoginUser();
      MySqlDataReader login = cmd.ExecuteReader();
  
       while(login.Read())
       {
               connect.Close();
               usr.UserID = login["UserID"];
               usr.valid = true;
       }    
       return usr;         
  }
  
  
  
      private void button1_Click(object sender, EventArgs e)
      {
          {
              string user = usertype.Text;
              string pass = password.Text;
  
  
              if (user == "" || pass == "")
              {
                  MessageBox.Show("Empty Fields Detected ! Please fill up all the fields");
                  return;
              }
              bool r = validate_login(user, pass);
              if (r)
              {
                  LoginUser usr = new LoginUser();
                  usr = validate_login(user, pass);
                  if (usr.valid)
                  {
                      Console.WriteLine(String.Format("{0}", usr.UserID));
                      UserDetails.m_gnUserId = Convert.ToInt32(reader["UserID"]);
                  }
              }
          }
      }
  </code></pre>