无法找到错误

时间:2014-04-01 10:51:08

标签: c#

我的程序出错,我无法解决问题。当我尝试将用户输入分配给我班级中的变量时,会出现此问题。

问题中的两个变量是:Quantity_Of_Order(string)& Date_Of_Purchase(string)

使用Quantity_Of_Order,我尝试为其分配在文本框中输入的值。 使用Date_Of_Purchase,我试图为它分配在日期时间选择器中选择的值。

我尝试输出值并显示:

  

订单数量:System.Windows.Forms.TextBox,Text:200   购买日期:System.Windows.Forms.DateTimePicker,价值:01/04/2014

当我尝试使用Quantity_Of_Order时,我的程序停止工作,我收到此消息:

  

mscorlib.dll中发生了未处理的“System.FormatException”类型异常   附加信息:输入字符串的格式不正确

这是我的输入代码,并在我的类中为变量赋值:

//This method sets the value of Quantity_Of_Order based on the amount entered by the user
    void Set_Quantity_Of_Order(int identifier)
    {
        string quantity = txtQuantityOrdered.ToString();//txtQuantityOrdered is the text box where user enters value
        Order[identifier].Quantity_Of_Order = quantity;
    }

//This method sets the value of Date_Of_Purchase, based on the date entered by the user
    void Set_Date_Of_Purchase(int identifier)
    {
        string purchase_date = dtpPurchaseDate.ToString();//dtpPurchaseDate is the date time picker where user selects date of purchase.
        Order[identifier].Date_Of_Purchase = purchase_date;
    }

我确信我刚刚犯了一个noob错误,因为我是C#的新手。您可以提供任何帮助。

由于

1 个答案:

答案 0 :(得分:4)

您不应使用txtQuantityOrdered.ToString();,而是

txtQuantityOrdered.Text

dtpPurchaseDate.Text

或者

dtpPurchaseDate.Value

最后一个输出DateTime个对象。