-1

I have a program that content two form the first form is the main form and is the form that the program will start form it and the second form is the form that will show one time only to take to values and then will comeback to the first and the second will close note :in the start the program will do one from two

  1. show second form if the values not in the program , then close the second and go to the first.
  2. show the first and counties on it if the program have the values .

Note: when the second form will appear the first form will be hide , then after program takes the values from the user the second form will close and the first form will show

in anther word if the user give to the program the values from before the second form will not appear ,just the first form will appear only

the problem is the second form still open when the first form open after second form took the values

Code form 1

private void Form1_Load(object sender, EventArgs e)
{
    b();
}

private void b()
{
    Open();
    String SqlQuery = "SELECT * from test1";
    DataTable dt = new DataTable();
    dt = fetchData(SqlQuery);
    if (!(dt.Rows.Count >0))
    {
        MessageBox.Show("incorect user");
        fm2 = new Form2();                
        fm2.Show();
        this.Hide();
    }
}

code form2

public void fn()
{
    String SqlQuery = "SELECT * from test1 ";
    DataTable dt = new DataTable();
    dt = fetchData(SqlQuery);
    if (dt.Rows.Count > 0)
    {
        Application.OpenForms[0].Show();
        MessageBox.Show("DONE");
        this.Close();
    }
}

private void button1_Click(object sender, EventArgs e)
{
    string insertQuery = "INSERT INTO test1( name, phone) VALUES" +
        "('" + (textBox1.Text) + "','" + (textBox2.Text) + "')";

    ExcuteQuery(insertQuery);         
    fn();
}

And I have a question that is the form1 that I make it hide run in the background after the code Application.OpenForms[0].Show();

Dale K
  • 25,246
  • 15
  • 42
  • 71
hana
  • 27
  • 5
  • FYI, there's no point in assigning `dt = new DataTable();` if you're just going to reassign it in the next line...you can just do `DataTable dt = fetchData(SqlQuery);` – Rufus L Jun 04 '21 at 18:00
  • Also, your code is wide open to SQL injection; please use named arguments instead of concatenating text from the form into a query string. – Rufus L Jun 04 '21 at 18:02

3 Answers3

1

If not really looked in the details of your app. But if you do not want to open Form1 until Form2is closed, you should change:

fm2.Show();

to

fm2.ShowDialog();
Luuk
  • 12,245
  • 5
  • 22
  • 33
  • when I changed it to fm2.ShowDialog(); the form doesn't close – hana Jun 04 '21 at 17:09
  • Then you need to debug your code, see: [Tutorial: Learn to debug C# code using Visual Studio](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-debugger?view=vs-2019). And, please, be more precise, you have two forms, and you say "the form doesnt close" .... which of the two forms does not close? – Luuk Jun 04 '21 at 17:49
  • 1
    BTW: When you debug your code you should find out that this line should be deleted `Application.OpenForms[0].Show();` – Luuk Jun 04 '21 at 17:52
  • sorry ,I didn't notice that I didn't say which form didn't close. the second form2 that isn't closed – hana Jun 04 '21 at 20:25
  • almost I know, but I didn't find a command to open the form1.. So I say i will test this way – hana Jun 04 '21 at 20:31
  • While `Form1_Load` is being executed the Form1 is not shown. You can see this by putting a breakpoint on the line `this.Hide();` . Form1 is not yet show when this code is reached, this code should hide the Form1, but when `Form1_Load` finished the form Form1 is shown. see, for another example, this: https://stackoverflow.com/questions/3742709/this-visible-is-not-working-in-windows-forms – Luuk Jun 05 '21 at 09:16
0
if (!(dt.Rows.Count > 1))

change to

if (!(dt.Rows.Count >0))
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
greg b
  • 13
  • 4
0

You could try hiding the main form and then showing the second form as a dialog (which means the code flow goes to the second form until it's closed, then resumes where it left off). You could even do it in a loop, in case they close the second form without entering a user:

Form1

string SqlQuery = "SELECT * from test1 ";
DataTable dt = fetchData(SqlQuery);

while (dt.Rows.Count == 0)
{
    this.Hide(); 
    MessageBox.Show("No users exist. Please add one.");    

    using (Form2 fm2 = new Form2())
    {
        fm2.ShowDialog(this);
    }

    dt = fetchData(SqlQuery);
}

this.Show();

And then in the second form, you don't have to worry about trying to show the first form, because the first form will show itself when the code resumes.

Form2

if (dt.Rows.Count > 0)
{
    MessageBox.Show("User added successfully.");
    this.Close();
}
else
{
    MessageBox.Show("No user was added. Please try again.");
}

If you're still having an issue, then it seems like either fetchData or ExecuteQuery aren't working correctly. You should verify that a user is actually added to SQL after ExecuteQuery.

Also you can add a breakpoint in your code and examine the Count property of dt.Rows in the debugger to ensure that fetchData is working.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • I try your code and its work good but after save the values to database the second form is not closed – hana Jun 04 '21 at 20:47
  • Please provide some more information - do you mean `this.Close()` does not close the form? Or do you mean that `Form2` still opens when there is a record in the database? – Rufus L Jun 04 '21 at 21:34
  • the **Form2** still opens while in the database the row has been added – hana Jun 04 '21 at 21:37
  • do you open form 2 anywhere else in your code? what does `Open` do? – Rufus L Jun 04 '21 at 21:40
  • no ,I don't open it . Function **Open** just to open the connection only – hana Jun 04 '21 at 21:42
  • Then that must mean the `dr.Rows.Count == 0` when you hit the `while` statement. Set a breakpoint and step through the code to see where the values aren't getting updated correctly. – Rufus L Jun 04 '21 at 21:58