Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one,



how would i show my user name to another page top of the form using c# windows application?

that means form1 name is form1

and form2 name is form2,

in my first form1 is having login page,

for example,

my user name : TEST

password : TEST@123

my problem is how can i shifted to my user name to other page like session in asp.net but this is for windows application.

Actually i done the above process,please see my below code but its showing in the page itself, i dont want such a condition.

i need only to show my user name in the top of my second page, that is my form name,

here my form name is Form2.

so please find out my code below which i mentioned and give me an idea were i put change the code....

i have two form.

form1.cs:

see my below code....

C#
 private void btnlogin_Click(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection(s);
                con.Open();
                cmd = new SqlCommand("select username,password from login",con);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string username = txtusername.Text;
                    string password = txtpassword.Text;
                    if (dr["username"].ToString() == username && dr["password"].ToString() == password)
                    {
                        Form2 hmpage = new Form2(username);
               
                        hmpage.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("The username or password you entered is incorrect!", "Logon Message!");
                    }


                }
                con.Close();
                dr.Close();
            }
            catch (Exception ex)
            {

            }

        }


 

Form2.cs:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tarriff_design_page
{
    public partial class Home : Form
    {
        public Home(string strTextBox)
        {
            InitializeComponent();
            label1.Text ="Welcome to:"+( strTextBox);
            
           
        }
        
       
    }
}


thanks in advance....
Posted
Updated 21-Feb-13 22:33pm
v2
Comments
Jibesh 22-Feb-13 4:40am    
what error you are getting.The user name you passed to the second form will be displayed where you placed the label.

change to this line.
label1.Text ="Welcome to:("+ strTextBox+")";

Hello friend,

You can do it in many ways, but i suggest you two easy ways, you can manage one global variable to store you user name, and its accessible in all over your application in any number of forms, and second one you already used, like while creating or calling second form pass that user name as constructor parameter of that form, make second constructor in your second form to get name from it and use it to show in you second form top part.


1) Using global variables at application level

2) Pass value in constructor of calling page
 
Share this answer
 
Hii buddy

after saw your q.s 1st i done it practically and after successfully debug now here for you

On Form1..................

C#
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;

namespace form1
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }
// this is your public class that contail your user name...
        public class shubh
        {
            public string shubham;
        }

      
 private void btnlogin_Click(object sender, EventArgs e)
        {
            try
            {
                con = new SqlConnection(s);
                con.Open();
                cmd = new SqlCommand("select username,password from login",con);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                   // string username = txtusername.Text;
                    //string password = txtpassword.Text;
                    if (dr["username"].ToString() == username && dr["password"].ToString() == password)
                    {
                        shubh s = new shubh();
                        s.shubham = txtusername.Text;
                        Form2 hmpage = new Form2(s);
                        hmpage.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("The username or password you entered is incorrect!", "Logon Message!");
                    }
 

                }
                con.Close();
                dr.Close();
            }
            catch (Exception ex)
            {
 
            }
 
        }
    }
}
Now on form2................

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tarriff_design_page
{
    public partial class Home : Form
    {
        form1.Form1.shubh s;
        public string sa;
        public Home(form1.Form1.shubh s1)
        {
            InitializeComponent();
            s = s1;

        }
 private void Form2_Load(object sender, EventArgs e)
        {
            sa =s.shubham ;
//on which control you want to show the username....
            label1.Text = s.shubham;
           
        }


    }
}


this will definitely run successfully but the main issue with your partial class of form one and form to here just i used form1 for 1st page and form2 for 2nd page so carefully change this nominations as your both forms that can help you to achieve your necessity ........ thanks


Happy to help
 
Share this answer
 
v2
Comments
Member 14013003 2-Jul-19 1:36am    
Thabks :)
you can pass as parameter while calling second form and create constructor in second form for retrieving the parameter.
Otherwise you can go for event and delegate.you can create a custom event and call the parameter when showing second form. in second form create a delegate to handle it.
 
Share this answer
 
v2

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