Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.

What I have tried:

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(); 
            }
      }
 }
Posted
Updated 27-Mar-19 2:50am

1 solution

Don't - that exposes the internal workings of the user control to the outside world.
Instead, see here: Transferring information between two forms, Part 1: Parent to Child[^] - it focusses on forms, but the principles (and even the code) are exactly the same for controls (because Form is derived from Control)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900