输入字符串格式不正确 - 解析?

时间:2017-10-20 17:52:45

标签: c# string parsing tostring tryparse

我有一个包含多个TextBoxes和ComboBoxes的页面,它们从用户那里获取输入。

然后,我将这些值传递给我们保存到我的数据库中。

单击“保存”时遇到错误:

System.FormatException: 'Input string was not in a correct format.'

以下是返回错误的代码:

Db_Helper.Insert(new Product(DescriptionBox.Text, int.Parse(QuantityBox.Text), UnitCombo.SelectedItem.ToString(),
                int.Parse(RecurrenceTimesCombo.SelectedItem.ToString()), RecurrenceEveryCombo.SelectedItem.ToString(), InterchangeCombo1.SelectedItem.ToString(),
                InterchangeCombo2.SelectedItem.ToString(), InterchangeCombo3.SelectedItem.ToString(), PickCatCombo.SelectedItem.ToString(),
                PickDietCombo.SelectedItem.ToString()));

以下是更大的图片:

private async void SaveButton_Click(object sender, RoutedEventArgs e)
    {

        DatabaseHelperClass Db_Helper = new DatabaseHelperClass();//Creating object for DatabaseHelperClass.cs from ViewModel/DatabaseHelperClass.cs    
        if (DescriptionBox.Text != "" & QuantityBox.Text != "" & UnitCombo.SelectedItem.ToString() != "" &
            RecurrenceTimesCombo.SelectedItem.ToString() != "" & RecurrenceEveryCombo.SelectedItem.ToString() != "" &
            PickCatCombo.SelectedItem.ToString() != "" & PickDietCombo.SelectedItem.ToString() != "")
        {
            Db_Helper.Insert(new Product(DescriptionBox.Text, int.Parse(QuantityBox.Text), UnitCombo.SelectedItem.ToString(),
                int.Parse(RecurrenceTimesCombo.SelectedItem.ToString()), RecurrenceEveryCombo.SelectedItem.ToString(), InterchangeCombo1.SelectedItem.ToString(),
                InterchangeCombo2.SelectedItem.ToString(), InterchangeCombo3.SelectedItem.ToString(), PickCatCombo.SelectedItem.ToString(),
                PickDietCombo.SelectedItem.ToString()));
            Frame.Navigate(typeof(ProductsPage));//after adding product redirect to products listbox page    
        }
        else
        {
            MessageDialog messageDialog = new MessageDialog("Please provide all data needed.");//Text should not be empty    
            await messageDialog.ShowAsync();
        }
    }

这是我的产品类:

public class Product
{

    [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
    public int Id { get; set; }
    public string CreationDate { get; set; }
    public string Name { get; set; }
    public int ProductQuantity { get; set; }
    public string ProductQuantityUnit { get; set; }
    public int RecurrenceTimes { get; set; }
    public string RecurrenceEvery { get; set; }
    public string Interchangeable1 { get; set; }
    public string Interchangeable2 { get; set; }
    public string Interchangeable3 { get; set; }
    public string Category { get; set; }
    public string DietID { get; set; }

    public Product(string name, int prodQuantity, string productQuantityUnit, 
        int recTimes, string recEvery, string interchange1, string interchange2, 
        string interchange3, string cat, string dietId)
    {
        CreationDate = DateTime.Now.ToString();
        Name = name;
        ProductQuantity = prodQuantity;
        ProductQuantityUnit = productQuantityUnit;
        RecurrenceTimes = recTimes;
        RecurrenceEvery = recEvery;
        Interchangeable1 = interchange1;
        Interchangeable2 = interchange2;
        Interchangeable3 = interchange3;
        Category = cat;
        DietID = dietId;


    }
}

我做错了什么? 构建时不会显示错误。它仅在按钮单击时运行应用程序时显示。

1 个答案:

答案 0 :(得分:0)

是的,看起来您要传递给int.Parse的值之一不是整数。但是哪个值会引起问题? (可能是RecurrenceTimesCombo.SelectedItem.ToString(),它是项目的文本,而不是其数字值!?)

为了使调试更容易,并向用户提供有用的反馈,我建议在单独的步骤中进行解析,如果解析失败,则抛出有意义的异常。您可以使用TryParse代替Parse,如果无法解析该值,则返回false

此外,如果要确保输入不为空,请使用!string.IsNullOrWhiteSpace(QuantityBox.Text)代替QuantityBox.Text != "",后者不会测试null或仅包含空格{{ 1}}。

"    "