1

Below is my code for the login form. Etc, If i login as Jack, the next form will display Jack in label1. If login as david, then the next for will display david in label1. Just like using session in a web form.

The Button Login Method

private void btnLogin_Click(object sender, EventArgs e)
{
    //retrieve connection information info from App.config
    string strConnectionString = 
             ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
    //STEP 1: Create connection
    SqlConnection myConnect = new SqlConnection(strConnectionString);
    //STEP 2: Create command
    string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
    // Add a WHERE Clause to SQL statement
    strCommandtext += "   WHERE dUsername=@dname AND dPassword=@dpwd;";
    SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
    cmd.Parameters.AddWithValue("@dname", textUsername.Text);
    cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);

    try {            
        myConnect.Open(); // STEP 3: open connection 
        SqlDataReader reader = cmd.ExecuteReader(); // retrieve data 
        while (reader.Read()) //For Doctor             
        {
            if (MessageBox.Show("Login Successful") == DialogResult.OK)
            {
                timer1.Enabled = true;
                return;
            }
        }

        //STEP 5: close connection
        reader.Close();
        MessageBox.Show("Invalid username or password");
    }
    catch (SqlException ex) { }
    finally {
        //STEP 5: close connection
        myConnect.Close();
    }
}

Login form display username in next form displayusername form error

Pony
  • 401
  • 3
  • 13
  • 43
  • Add a property `UserName` in `displayusername` form, set the username, then show it – Sriram Sakthivel Jan 04 '14 at 11:06
  • what's property? @Sriram Sakthivel – Pony Jan 04 '14 at 11:09
  • @Jordjmax very naive question, keep learning :). Properties are like fields, it provides encapsulation. You'll have used `form.Text` `Text` is a property. http://www.dotnetperls.com/property – Sriram Sakthivel Jan 04 '14 at 11:12
  • I can see no difference to the questions regarding [passing value from one form to another](http://stackoverflow.com/search?q=%5Bc%23%5D+pass+values+from+one+form+to+another) or [how to implement login form](http://stackoverflow.com/questions/14375892/how-to-implement-login-form-and-main-form-without-more-instances-in-c-sharp)- possible duplicate. – surfmuggle Jan 04 '14 at 11:17
  • possible duplicate of [how to pass values from one form to another](http://stackoverflow.com/questions/818930/how-to-pass-values-from-one-form-to-another) – surfmuggle Jan 04 '14 at 11:18

3 Answers3

2

If passing the name to next form only is your goal, then you can create a constructor for the new form and pass that value in the constructor call itself. This value can be then used in the new form.
As an example

Form2 frm2 = new Form2("LoginName");

This can be then used in the custructor for Form2.

public Form2(string loginname)
{
_loginname=loginname;
}

This can be then used in form_load as

   Label1.Text=_loginname;

Otherwise, using a static variable is also an option for you.
Let me know if you want some help with the code.

Vikram Sharma
  • 377
  • 1
  • 8
  • 21
  • Sorry I have no idea how to do that, can show me some codes? @Vikram Sharma – Pony Jan 04 '14 at 11:10
  • I am trying now. Vignesh Kumar – Pony Jan 04 '14 at 11:14
  • For your instruction on this (now show FormTwo on click on FormOne By creating object of FormTwo, we can Access global veriable and assign values to it) Do i do it on the private void timer1_Tick instead? Because currently for my loading of displayusername form codes is placed in timer1_tick. @Vignesh Kumar – Pony Jan 04 '14 at 11:16
  • @Jordjmax, you can pass a value to the constructor wherever you are creating a new instance of the form. – Vikram Sharma Jan 04 '14 at 11:20
1

You can think of adding a public class and declaring public static string as its member as below .

public class UserDisplayName { public static string displayName; }

Now, where you show the 'Login Successful' message, there you now set your username as

UserDisplayName.displayName = textUsername.Text;

And to show the username back on the form, use it like

label1.Text = UserDisplayName.displayName;

Thus, it will now set your username.Benifit of using this is that, you can now use this UserDisplayName.displayName on all forms in your entire application so you don't have to re-declare and set variable on all forms.Basic idea behind this is that there is no concept of global variable in c#. Purpose of global variable is served by static data member of a class.

Omkar Manjare
  • 53
  • 1
  • 5
0

make the label of form2 public and on form1 assign the value to that label or the second method u can give the form2.name=username(you retrieved from database) and in form2 get the name.. like this.Name