0

I've tried the methods from

Getting values from two or more forms C# Sharing a variable between two winforms Get value from parentform and passing username to form but username returns null c#

But i couldnt get mine to work as i will get some random argument error.

the two forms From the picture , i just want the username on Login form to be put into label1 on User form.

But also be able for other forms to use later as well.

Can someone help with this?

Login Form Code :

public partial class Login : Form
    {
        UserForm _userform = new UserForm();
        Admin _Adminform = new Admin();

        public Login()
        {
            InitializeComponent();
        }

        private void loginscs_Click(object sender, EventArgs e)
        {
            try
            {
                string userNameText = txtUser.Text;
                string passwordText = txtPass.Text;
                string isAdmin = "yes";
                string isNotAdmin = "no";
                if (!(string.IsNullOrEmpty(txtUser.Text)) && !(string.IsNullOrEmpty(txtPass.Text)))
                {
                    SqlConnection SCScon = new SqlConnection();
                    SCScon.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
                    SqlCommand cmd = new SqlCommand("SELECT ISNULL(SCSID, '') AS SCSID, ISNULL(SCSPass,'') AS SCSPass, ISNULL(isAdmin,'') AS isAdmin FROM SCSID WHERE SCSID='" + txtUser.Text + "' and SCSPass='" + txtPass.Text + "'", SCScon);
                    SCScon.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
                            this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
                            this.CompareStrings(dr["isAdmin"].ToString(), isAdmin))
                        {
                            MessageBox.Show("Hello " + txtUser.Text, "Admin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            _Adminform.Show();
                            this.Hide();
                        }
                        else if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
                            this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
                            this.CompareStrings(dr["isAdmin"].ToString(), isNotAdmin))
                        {
                            MessageBox.Show("Welcome " + txtUser.Text, "User");
                            _userform.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Wrong ID/Pass");
                    }
                    SCScon.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("error2" + ex);
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private bool CompareStrings(string string1, string string2)
        {
            return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
        }
    }

===========================

Here , i've tried this , but this , did not work..

that thing

Community
  • 1
  • 1
  • #Grant Winney @Grant Winney - Why do i get a duplicate when the date is 4 years difference , as well as the coding method as well as the problem. The title might almost be the same , but the problem is different. I have also provided other links above. Please check before marking as duplicate. – Mohd Nasrul Iwan Fajaruddin Feb 26 '15 at 06:08

3 Answers3

0

Create a consturctor for the second form and in it's creation (calling new Form2(...)) - send as parameters the user name an / or the password.

Javier
  • 12,100
  • 5
  • 46
  • 57
Tal Malaki
  • 422
  • 5
  • 18
  • i have no idea how to do that.. Sorry , i have low basic , but i do understand the code.. mind teaching me? i just dont get the terms.. like , a constructor.. have no idea what is that. – Mohd Nasrul Iwan Fajaruddin Dec 29 '13 at 07:52
  • A constructor is like a factory that creates your class. You can have a deafult parameter-less constructor that is called like this: Form F1 = new From(). Or, you can create a constructor of your own - it's an overload (try reading about it). If you create a new constructor and give it 2 string params for example you can call the form like this: Form2 F2 = New Form2(word1,word2); – Tal Malaki Dec 29 '13 at 07:56
  • ah.. okay.. so my username from first form is txtUser , so in UserForm , i create a UserForm F2 = new UserForm(txtUser.Text);? like this? – Mohd Nasrul Iwan Fajaruddin Dec 29 '13 at 08:01
  • but how do i make txtUser from LoginForm detectable in UserForm? – Mohd Nasrul Iwan Fajaruddin Dec 29 '13 at 08:02
  • You need to add a method to the UserForm. like this: pubilc UserForm (string userName, string Passowrd) { some code...}. Try reading in the net about overloading and constructor as well. – Tal Malaki Dec 29 '13 at 08:05
  • http://i.imgur.com/oek52H3.png here... i got this.. – Mohd Nasrul Iwan Fajaruddin Dec 29 '13 at 08:14
0

You can add constructor that get parameters to UserForm and give some object that in the constructor will have the object.

you also can add getter/setter in the Userform and update the information that you want to pass.

Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • i have no idea how to do that.. Sorry , i have low basic , but i do understand the code.. i just dont get the terms.. like , a constructor.. have no idea what is that.. mind teaching me? – Mohd Nasrul Iwan Fajaruddin Dec 29 '13 at 07:39
0

You can, for example, simply use a property:

Form2:

public string UserText { get; set;}

...

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "")
    {
        MessageBox.Show("Please enter keyword to search");
    }
    else 
    {
        UserText = textBox1.Text; // set the Text
    }

Form1:

private void button5_Click(object sender, EventArgs e)
{            
    Form2 form2 = new Form2();
    form2.ShowDialog();   //open form2-search form  

    string text = from2.UserText; get the Text

    ....

This Answer is stated by Dominic Kexel Hope this help

Community
  • 1
  • 1
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115