none
How add value to a label in user control? RRS feed

  • Question

  • Hi! 

    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. It don't show the value. 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();
             } 
          }
    
    }
    
    
    
    
    
    public partial class HomepageUC : UserControl
    {
       string login = "";
    
       public HomepageUC()
       {
           InitializeComponent();
       }
    
       public HomepageUC(string email): this() 
          {
           login = email;
            var conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB; 
            AttachDbFileName=|DataDirectory|database.mdf;");
            conn.Open();
             try
                {
                  conn.Open();
                  var cmd = new SqlCommand($"SELECT name FROM registration_data WHERE email = '{login}'", conn);
                   var reader = cmd.ExecuteReader();
                   while (reader.Read()) labelWelcome.Text = reader[0].ToString();
                }
                
                finally
                {
                    conn.Close(); 
                }
          }
     }




              
    • Edited by nickname18 Wednesday, March 27, 2019 12:11 PM
    Wednesday, March 27, 2019 12:10 PM

All replies

  • Hi nikname18,

    Here I have simple demo you can refer to.

    Suppose I have created the following data table in the database.

    In this demo, Click "ToLogin" in the "Registration" form to enter "Login" form, then click "Login" to enter "Main" form.

    The specific code is shown as follows:

    Registration.cs:

        private void btToLogin_Click(object sender, EventArgs e)
        {
            Login login = new Login();
            login.ShowDialog();
        }

    UCWelcome.cs:

        public UCWelcome(string welcomestring):this()
        {
            this.label1.Text = "Welcome " + welcomestring + "!";
        }

    Login.cs:

        private void btLogin_Click(object sender, EventArgs e)
        {
            string welcomestring = "";
            string constring = @"connection string";
            string sqlstring = $"select name from UserTable where email='{textboxEmail.Text}'";
            using (SqlConnection sqlcon = new SqlConnection(constring))
            {
                SqlDataAdapter ada = new SqlDataAdapter(sqlstring, sqlcon);
                DataSet ds = new DataSet();
                ada.Fill(ds);
                welcomestring = ds.Tables[0].Rows[0]["name"].ToString();
            }
    
            Main main = new Main();
            UCWelcome uCWelcome = new UCWelcome(welcomestring);
            main.Controls.Add(uCWelcome);
            main.ShowDialog();
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 28, 2019 2:40 AM
    Moderator