参数在窗口窗体中无效异常
本文关键字:无效 异常 窗体 窗口 参数 | 更新日期: 2023-09-27 18:31:13
我正在使用多面板来制作多部分项目并使用访问数据库并在其中插入一些内容,如下所示:
private void addmoneyPanel_firstLoad()
{
try
{
employee_list.Items.Clear();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select Ename,Elastname from employee";
OleDbDataReader reader = command.ExecuteReader();
while(reader.Read())
{
employee_list.Items.Add(reader[0].ToString() + " 'n" +reader[1].ToString());
}
connection.Close();
/*addMoneyPanelMes.Text = "با موفقیت ذخیره شد.";
addMoneyPanelMes.ForeColor = Color.Green;*/
}
catch (Exception err)
{
addMoneyPanelMes.Text = "خظا در ارتباط با پایگاه داده.";
addMoneyPanelMes.ForeColor = Color.Red;
addMoneyPanelMes.Visible = true;
}
}
private void pictureBox1_Click_1(object sender, EventArgs e)
{
try
{
string TempPrice, TempCheckNum, TempCriditNum;
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
if(radioButton1.Checked == true)
{
TempPrice = money_price.Text;
TempCheckNum = "0";
TempCriditNum = "0";
}else if(radioButton2.Checked == true)
{
TempPrice = money_price.Text;
TempCheckNum = "0";
TempCriditNum = criditNumber.Text;
}
else
{
TempPrice = money_price.Text;
TempCheckNum = checkNumber.Text;
TempCriditNum = "0";
}
///////////////////////////////split the combo box names
string mainToSplit,id = "";
string[] splited;
mainToSplit = employee_list.SelectedItem.ToString();
splited = mainToSplit.Split(''n');
splited[0] = "" + splited[0].Remove((splited[0].Length-1),1);
command.CommandText = "select id from employee where Ename='" +splited[0]+ "' AND Elastname='" +splited[1]+"'";
OleDbDataReader reader = command.ExecuteReader();
if (reader.Read())
id = reader[0].ToString();
connection.Close();
connection.Open();
OleDbCommand command2 = new OleDbCommand();
command2.Connection = connection;
command2.CommandText = "INSERT INTO realMoney (price,cardnum,checknum,theDate,employeeid,descrip) values(" + Int32.Parse(TempPrice) + "," + Int32.Parse(TempCriditNum) + "," + Int32.Parse(TempCheckNum) + ",#" + dateTimePickerX1.GetSelectedDateInPersianDateTime().ToShortDateString() + "#," + Int32.Parse(id) + ",'" + money_descrip.Text + "')";
command2.ExecuteNonQuery();
connection.Close();
addMoneyPanelMes.Text = "با موفقیت ذخیره شد.";
addMoneyPanelMes.ForeColor = Color.Green;
addMoneyPanelMes.Visible = true;
}
catch(OleDbException h)
{
addMoneyPanelMes.Text = "خظا در ارتباط با پایگاه داده.";
addMoneyPanelMes.ForeColor = Color.Red;
addMoneyPanelMes.Visible = true;
}
}
这个 2 函数将成功运行,但之后我将在此部分收到"参数无效"异常:
private void timer1_Tick(object sender, EventArgs e)
{
if(pass == 0 || pass == 1)
{
prossespass();
}
DateTime datetime = DateTime.Now;
try
{
time.Text = string.Format("{0:hh:mm:ss}", DateTime.Now); // error here
timesun.Text = datetime.ToString("tt");
}
catch(Exception d)
{ }
}
这是我项目中时钟的计时器。 因此,返回主面板后,其中存在时钟(使主面板可见并隐藏当前面板)无法设置时钟文本框,我的项目崩溃了。我不知道问题是什么。但是,如果我从我提到的第二个函数中删除这部分:
addMoneyPanelMes.Text = "با موفقیت ذخیره شد.";
addMoneyPanelMes.ForeColor = Color.Green;
addMoneyPanelMes.Visible = true;
或删除上述第二个功能中的插入部分:
connection.Open();
OleDbCommand command2 = new OleDbCommand();
command2.Connection = connection;
command2.CommandText = "INSERT INTO realMoney (price,cardnum,checknum,theDate,employeeid,descrip) values(" + Int32.Parse(TempPrice) + "," + Int32.Parse(TempCriditNum) + "," + Int32.Parse(TempCheckNum) + ",#" + dateTimePickerX1.GetSelectedDateInPersianDateTime().ToShortDateString() + "#," + Int32.Parse(id) + ",'" + money_descrip.Text + "')";
command2.ExecuteNonQuery();
connection.Close();
我在其他面板中还有其他功能,可以与数据库一起使用,但可以很好地协同工作,就是这样。 感谢您的帮助。
只需关闭计时器:
timer1.Enabled = false;
当进入每个面板并在返回主面板时将其打开时:
timer1.Enabled = true;