简体   繁体   中英

How pass a value to user control from a winform?

I have a registration form, a login form and a Main form.The program start with registration form. If i sign up the datas(name, email, password) load up to the local database. When i log in correctly show the main form. The main form has a usercontrol with a label. I would like to write a welcome text to the label with her/his name. Example: "Welcome Josh!". So I should to identify the user, so i use the textboxEmail.Text from the login form. My solution is not working. There is my code:

namespace personalFinance
{
   public partial class Login : Form
      {
        public Login()
         {

            InitializeComponent();
            var MainForm = new MainForm();
            MainForm.Show();
            HomepageUC hp = new HomepageUC(textboxEmail.Text);
            hp.Show();
         } 
      }

}
namespace personalFinance
{
    public partial class HomepageUC : UserControl
    {
       string login = "";
       public HomepageUC(string email)
          {

            InitializeComponent();
            login = email;
            var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB; 
            AttachDbFileName=|DataDirectory|database.mdf;");
            conn.Open();
            var cmd = new SqlCommand($"SELECT email FROM registration_data 
            WHERE email = '{login}'", conn);
            var reader = cmd.ExecuteReader();
            while (reader.Read()) labelWelcome.Text = reader[0].ToString();
          }
     }

 }

I got that error: There is no argument given that corresponds to the required formal parameter 'email' of 'HomepageUC.HomepageUC(string)' personalFinance C:\\Users\\nickname18\\source\\repos\\personalFinance\\personalFinance\\MainForm.Designer.cs

when i click this error retrieve to MainForm.Designer.cs this.HompageUC1 = new personalFinance.Homepage1(); it is underline with red.

The WinForms designer creates user controls by calling their default constructor. Therefore, you can't define a custom constructor like that.

Instead, you should create a custom property.

Is the email field unique? Did you already debug? Are occurs any error? Perhaps the query brought more than one record or no record, and perhaps the field was empty too. Try this:

namespace personalFinance
{
    public partial class HomepageUC : UserControl
    {
       string login = "";
       public HomepageUC(string email)
          {

            InitializeComponent();
            login = email;
            var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB; 
            AttachDbFileName=|DataDirectory|database.mdf;");
            conn.Open();
            var cmd = new SqlCommand($"SELECT email FROM registration_data 
            WHERE email = '{login}'", conn);
            var reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                if(!string.IsNullOrEmpty(reader[0].ToString()))
                {
                    labelWelcome.Text = reader[0].ToString();
                    break;
                }
            }           
        }
    }
}

You should use parametrized queries and do proper usage of unmanaged resources. An easy and quick trick to IDisposable is the using keyword

public partial class HomepageUC : UserControl
{
   string login = "";

   public HomepageUC() // Default constructor for the designer/properties inicialization
   {
       InitializeComponent();
   }

   public HomepageUC(string email): this() // Your business logic constructor calls the default one
      {

        login = email;
        var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB; 
        AttachDbFileName=|DataDirectory|database.mdf;");
        conn.Open();
        var cmd = new SqlCommand($"SELECT email FROM registration_data WHERE email = @email", conn);
        cmd.Parameters.AddWithValue("@email", email); // Use parameters to avoid SQLi 
        var reader = cmd.ExecuteReader();
        while (reader.Read()) labelWelcome.Text = reader[0].ToString();
        conn.Close(); // Added unmanaged resources liberation
        conn.Dispose();
      }
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM