3
votes

I have two forms in my project (Form1 & Form2).

On Form1 I have two textBoxes like "Login" Form where user is asked to enter Username (textboxUsername) & Password (textboxPassword). When user is logged in, Form2 pops up.

Now in Form2 I have a button1 and dataGridView1. When I press button1 I want to show the selected user which I entered in Form1 (textboxUsername)

I want to get that user based on his Username which is saved in SQL Server.

Form2 button1 code:

 private void button1_Click(object sender, EventArgs e)
    {
        string constring = @"Data Source=V-K\;Initial Catalog=ATMKlientet;Integrated Security=True";
        SqlConnection conDataBase = new SqlConnection(constring);
        SqlCommand cmdDataBase = new SqlCommand(" select * from Clients ;", conDataBase);

        try
        {
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmdDataBase;
            dbdataset = new DataTable();
            sda.Fill(dbdataset);
            BindingSource bSource = new BindingSource();

            bSource.DataSource = dbdataset;
            dataGridView1.DataSource = bSource;
            sda.Update(dbdataset);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

In this case, the part where I want to call Form1 textBoxUsername, in Form2 is:

(" select * from Clients where Username='" + this.textBoxUsername + "';",  conDataBase);
2
The basic answer to this question is don't. Have a user class and link it up through events (an interface would be good as well), then you end up with both forms 'knowing' the login interface and nothing about each other. - Tony Hopkinson

2 Answers

4
votes

In Form1 when you open the second form, probably you have

 Form2 f = new Form2();
 f.ShowDialog();

here you could change the call passing the value of the textbox to the constructor of Form2;

 Form2 f = new Form2(textBoxUserName.Text);
 f.ShowDialog();

Inside the Form2 constructor you receive this value and store it in a class level global variable

public class Form2: Form
{
    private string currentUserName = string.Empty;
    public Form2(string userName)
    {
        currentUserName = userName;
    }
}

Now you could use the internal private variable currentUserName for your queries

As a side note, do not use string concatenation to build sql commands. Use always a parameterized query:

SqlCommand cmd = new SqlCommand("select * from Clients where Username=@uname", conDataBase);
cmd.Parameters.AddWithValue("@uname", currentUserName);
.....

using string concatenation is dangerous because your code becomes vulnerable to Sql Injections attacks and you need to take appropriate measure for string, decimal and date parsing. (For example, if the username contains a single quote the string concatenation will generate an invalid SQL statement without a proper replace of the single quote with a couple of quotes)

0
votes

A new DB request is not necessary, as you you have already the user name in Form, you just have to sent it to Form2 via the appropriate constructor:

In Form2, add a new constructor:

public void Form2 (String userName){...}

In Form1:

Form 2 = new Form2 (this.textBoxUsername.Text)