0

I'm trying to open another form with a button and it's only ok at the beginning. After a few forms made the stackoverflow error acours ! The error is on form 1, form 2 and form 3 (I started to debug multiple times): the codes are really simple. like for form 3:

public partial class Form2 : Form
{
    Form3 obrok = new Form3();
    public Form2()
    {
        InitializeComponent();
    }

    public void textBox1_TextChanged(object sender, EventArgs e)
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {
        obrok.Show();
        this.Hide();
    }
}

(added from the comment, especially to preserve code indentation (MI))

The above error is solved(using statement) Now the problem is when i wan't to save values from textbox to the access database. The code: Form1 users=new Form1(); private void button1_Click(object sender, EventArgs e) { string conString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\Users\Simon\Desktop\save.mdb";

        OleDbConnection empConnection = new OleDbConnection(conString);


        string insertStatement = "INSERT INTO obroki_save "
                             + "([ID_uporabnika],[datum],[ID_zivila],[skupaj_kalorij]) "
                             + "VALUES (@ID_uporabnika,@datum,@ID_zivila,@skupaj_kalorij)";

        OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);

        insertCommand.Parameters.Add("@ID_uporabnika", OleDbType.Char).Value = users.iDTextBox.Text;
        insertCommand.Parameters.Add("@datum", OleDbType.Char).Value = DateTime.Now;
        insertCommand.Parameters.Add("@ID_zivila", OleDbType.Char).Value = iDTextBox.Text;
        insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Char).Value = textBox1.Text;
        empConnection.Open();

        try
        {
            int count = insertCommand.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            empConnection.Close();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
        }
    }
}

When i wan't to pass the values it show a massage that there is an error in the data type in conditional expression .

5
  • I think we gonna need more details to help you. Can't you write the whole sample of code that causes this error? Commented Apr 28, 2010 at 8:59
  • It means that you asked too many questions on StackOverflow to develop your application. :P Out of joke, could you post some more code? Is there any recursion involved? Does Form5 too create other Form5 objects? Commented Apr 28, 2010 at 9:01
  • tnx for the quick answers ! upps. my mistake actually. The error is on form 1, form 2 and form 3(i started to debug multiple times): the codes are realy simple. like for form 3: public partial class Form2 : Form { Form3 obrok = new Form3(); public Form2() { InitializeComponent(); } public void textBox1_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { obrok.Show(); this.Hide(); }}} Commented Apr 28, 2010 at 9:12
  • You should have edited your question instead of putting the code in the comment, where it isn't formatted. This time I fixed it for you. Commented Apr 28, 2010 at 9:27
  • Since it is a completely different problem, you should create a new question, and maybe rollback this one to the last revision coherent with what you initially asked (I'd go with revision 4). Commented Apr 28, 2010 at 9:51

1 Answer 1

3

So, Form2 instances a new Form3 when it's constructed, Form3 instances a new Form5 on construction... maybe Form5 even instances yet another form, which instances another one and so on? In my opinion, you declared your classes in a way so that instancing one of them actually instances a lot of other form objects, and maybe there's even a circular reference (e.g. Form3=>Form5=>Form???=>...=>Form3=>Form5=>Form???=>... forever), so you're stuck in infinite recursion in the constructors of your forms.

If things are like that, if possible for your program logic, you should move your declarations of the forms inside the method that shows them, better if enclosed inside a using statement: in this way they get instanced only when they are needed, and are destroyed exactly when they are no longer used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.