title: How to check if user is log in once enter a form using c# winform only, no asp.netc#
body: <p>NO <strong>asp.net c#</strong> used</p>
  
  <p>I need to know if user is login once he/she enter a form and if he/she is not login, he goes back to the login form. Everywhere I saw people use asp.net to achieve that but I want to know how to do it without using asp.net.</p>
  
  <p>public DataTable Login(String Username, String Password)
          {</p>
  
  <pre><code>        server = "localhost";
          database = "xxxx";
          uid = "root";
          password = "";
  
          string MySQLConnectionString = $"datasource=127.0.0.1;port = 3306; SERVER={server}; DATABASE={database}; USERNAME={uid}; PASSWORD={password};sslmode=none";
  
          MySqlConnection db_Conn = new MySqlConnection(MySQLConnectionString);
  
          MySqlCommand cmd = new MySqlCommand();
  
          db_Conn.ConnectionString = MySQLConnectionString;
  
          string username_txtfield = username_txtbox.Text;
          string pw_txtfield = password_txtbox.Text;
  
          try
          {
  
              db_Conn.Open();
              cmd.Connection = db_Conn;
  
              cmd.CommandText = "SELECT count(*),user_id,person_username,role_id FROM user_tb " +
                  "WHERE person_username=@username " +
                  "AND user_password=@password";
  
              cmd.Prepare();
              cmd.Parameters.AddWithValue("@username", username_txtfield);
              cmd.Parameters.AddWithValue("@password", pw_txtfield);
  
              MySqlDataReader dr = cmd.ExecuteReader();
  
              DataTable dt = new DataTable();
              dt.Load(dr);
              db_Conn.Close();
  
              return dt;
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  </code></pre>
  
  <p>login btn in my login form</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          //login();
          try
          {
              DataTable result = Login(username_txtbox.Text, password_txtbox.Text);
              if (result.Rows.Count == 1)
              {
                  this.Hide();
  
                  string role = result.Rows[0]["role_id"].ToString();
  
  
                  switch (role)
                  {
                      case "3":
                          MessageBox.Show("User login successfully!");
                          user_form_page();
                          break;
  
                      case "1":
  
                          MessageBox.Show("Admin login successfully!");
                          //this.Hide();
                          Admin_page admin_form = new Admin_page();
                          admin_form.ShowDialog();
                          this.Close();
                          break;
                  }
              }
              else
              {
                  MessageBox.Show("INVALID USERNAME OR PASSWORD");
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
  
      }
  </code></pre>