表单的 System.StackOverflowException
我试图用按钮打开另一个表单,但一开始就可以。经过几个表格后,出现了 stackoverflow 错误! 错误出现在表格1、表格2和表格3上(我开始调试多次):代码非常简单。就像表单 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();
}
}
从注释中添加,特别是为了保留代码缩进(MI))
上述错误已解决(使用语句)现在问题是当我不想保存文本框中的值时到访问数据库。代码: Form1 用户=new Form1(); 私人无效button1_Click(对象发送者,EventArgs e) { 字符串 conString =“Provider=Microsoft.Jet.OLEDB.4.0;” + "数据源=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();
}
}
}
当我不想传递值时,它会显示一条消息,表明条件表达式中的数据类型存在错误。
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 .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,Form2 在构造时实例化一个新的 Form3,Form3 在构造时实例化一个新的 Form5...也许 Form5 甚至实例化另一个表单,该实例又实例化另一个表单,依此类推?在我看来,您以某种方式声明了您的类,以便实例化其中一个实际上实例化了许多其他表单对象,甚至可能存在循环引用(例如 Form3=>Form5=>Form???=> ...=>Form3=>Form5=>Form???=>... 永远),因此您在表单的构造函数中陷入了无限递归。
如果事情是这样的,如果您的程序逻辑可能的话,您应该将表单的声明移动到显示它们的方法中,最好将其包含在 using 语句中:这样,它们仅在需要时才会被实例化,并且当它们不再使用时就被销毁。
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.