参数无效windowsForm中的异常

时间:2016-01-21 08:22:00

标签: c# exception

我使用多面板制作多节项目并使用访问数据库并在其中插入一些内容,如下所示:

    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();

我在其他面板中有其他功能可以与数据库配合使用,但是它可以很好地协同工作。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

关闭计时器:

timer1.Enabled = false;

进入每个面板并在返回主面板时将其打开:

timer1.Enabled = true;