locked
How to display the username after login (C# windows form application) RRS feed

  • Question


  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    
    namespace Login
    {
        public partial class login : Form
        {
            public login()
            {
                InitializeComponent();
            }
    
            private void label2_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=H:\clean up\project C#\Login\Login\Database1.mdf;Integrated Security=True;User Instance=True");
    
    
                
    
                if ((textBox1.Text != "") && (textBox2.Text != ""))
                {
                    string username1 = textBox1.Text;
                    string password1 = textBox2.Text;
    
    
    
    
    
                    con.Open();
                    string sel = "SELECT username,password FROM student WHERE (username = '" + username1 + "')";
                    SqlCommand user = new SqlCommand(sel, con);
                    SqlDataReader data = user.ExecuteReader();
    
                    if (data != null)
                    {
                        while (data.Read())
                        {
    
    
    
    
    
                            string username = null, password = null;
                            username = data[0].ToString();
                            password = data[1].ToString();
    
                            if ((username1 == username) && (password1 == password))
                            {
    
                                student_main main = new student_main(username);
    
                                main.Show();
                                this.Hide();
                                con.Close();
    
    
                            }
                            else
                            {
                                MessageBox.Show("Please Check your Username & Password");
                                con.Close();
    
                            }
                        }
                    }
                }
    
                else
                {
                    MessageBox.Show("Texts Boxs are empty");
                }
            }
                
               
        
    
            private void label1_Click(object sender, EventArgs e)
            {
                this.Hide();
                register register = new register();
                register.Show();
            }
    
            private void pictureBox3_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void eventLog1_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
            {
    
            }
        }
    }


    This my Login form please help.

    Thanks

    Senu


    • Edited by Senu Friday, October 28, 2016 4:38 AM
    Friday, October 28, 2016 3:50 AM

Answers

  • Hi

       In order to display the username in Title bar of another form (to which the user is directed) use the below code

          if(role=="student1")
          {
                               Form1 f1 = new Form1();
                               f1.Text= textBox1.Text;
                               f1.Show();
          }
    
           if(role=="student2")
           {
                               Form2 f2 = new Form2();
                               f2.Text= textBox1.Text;
                               f2.Show();
           }
    
           if(role=="student3")
           {
                               Form3 f3 = new Form3();
                               f3.Text= textBox1.Text;
                               f3.Show();
            }
                                this.Hide();
                                con.Close();
    
     

    If this post is helpful to you please mark it as answer as it would be helpful for those who face the same issue in future.

    Thanks.

    • Marked as answer by Senu Friday, October 28, 2016 6:52 AM
    Friday, October 28, 2016 6:18 AM