2013-03-14 29 views
-3

我正在嘗試編寫一個簡單的GUI應用程序,用於在單擊檢索按鈕時從Customer類加載客戶數據。然後,當點擊保存按鈕時,這些數據將存儲在Customer對象中。C#:從字符串轉換爲DateTime時出現未處理的異常

我通過測試發現,我清晰和檢索按鈕按預期工作,但我收到以下錯誤消息,當我按下保存按鈕:

未處理的異常發生在您的應用程序。如果您單擊繼續,應用程序將忽略此錯誤並嘗試繼續。如果您單擊退出,應用程序將立即關閉。

該字符串未被識別爲有效的DateTime。有一個未知的單詞從索引4開始。

我粘貼了下面的代碼。我希望你們能提供的任何澄清。先謝謝你!

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace simple_GUI 
{ 

    public partial class Form1 : Form 
    { 

     Customer myCustomer = new Customer(); //Declare an instance of the customer class within my Windows form 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void saveButton_Click(object sender, EventArgs e) 
     { 
      myCustomer.Id = Convert.ToInt32(customerIDTextBox.Text); //How to handle exceptions within my conversions? 
      myCustomer.Name = fullNameTextBox.Text; 
      myCustomer.Address = addressTextBox.Text; 
      myCustomer.Email = emailTextBox.Text; 
      myCustomer.Phone = Convert.ToDouble(phoneNumberTextBox.Text); 
      myCustomer.AddDate = Convert.ToDateTime(addressTextBox.Text); 
      myCustomer.TotalTransactions = Convert.ToInt32(totalTransactionsTextBox.Text); 


     } 

     private void retrieveButton_Click(object sender, EventArgs e) 
     { 
      customerIDTextBox.Text = Convert.ToString(myCustomer.Id); 
      fullNameTextBox.Text = myCustomer.Name; 
      addressTextBox.Text = myCustomer.Address; 
      emailTextBox.Text = myCustomer.Email; 
      phoneNumberTextBox.Text = Convert.ToString(myCustomer.Phone); 
      addDateTextBox.Text = Convert.ToString(myCustomer.AddDate); 
      totalTransactionsTextBox.Text = Convert.ToString(myCustomer.TotalTransactions); 

     } 

     private void clearButton_Click(object sender, EventArgs e) 
     { 
      customerIDTextBox.Text = ""; 
      fullNameTextBox.Text = ""; 
      addressTextBox.Text = ""; 
      emailTextBox.Text = ""; 
      phoneNumberTextBox.Text = ""; 
      addDateTextBox.Text = ""; 
      totalTransactionsTextBox.Text = Convert.ToString(""); 

     } 
    } 
} 
+1

什麼是addressTextBox的文本(也許這是關鍵:將地址字符串轉換爲datetime)?錯誤非常明顯:字符串未被識別爲有效的DateTime – prime23 2013-03-14 03:34:21

+1

而且我建議您使用DateTimePicker控件讓用戶輸入日期而不是文本框 – prime23 2013-03-14 03:41:16

+0

您使用的日期字段格式是什麼? – ecampver 2013-03-14 03:42:07

回答

0

你可以檢查IFIT可能之前設置的值直接轉換,使用類型的TryParse()方法轉換,你可以採取正確的轉換,嘗試這樣的事情

private void saveButton_Click(object sender, EventArgs e) 
{ 
    // check if the Id is a integer 
    int convertedId; 
    if (!int.TryParse(customerIDTextBox.Text, out convertedId)) 
    { 
     MessageBox.Show("The ID is not a integer value"); 
     return; 
    } 

    myCustomer.Id = convertedId; 
    myCustomer.Name = fullNameTextBox.Text; 
    myCustomer.Address = addressTextBox.Text; 
    myCustomer.Email = emailTextBox.Text; 
    myCustomer.Phone = phoneNumberTextBox.Text; 

    // check if the DateTime is a valid dateTeime 
    DateTime convertedDate; 
    if (!DateTime.TryParseExact(addressTextBox.Text,"MM/dd/yyyy", null, System.Globalization.DateTimeStyles.AssumeLocal, out convertedDate)) 
    { 
     MessageBox.Show("The filed is not a valid date"); 
     return; 
    } 

    myCustomer.AddDate = convertedDate; 


    myCustomer.TotalTransactions = Convert.ToInt32(totalTransactionsTextBox.Text); 
} 
+0

這是一個很好的例子!非常感謝! – user2168112 2013-03-14 13:34:21

0

您可以通過添加一些代碼驗證輸入,取ADDDATE例如:

try{ 
    myCustomer.AddDate = Convert.ToDateTime(addressTextBox.Text); 
} 
catch 
{ 
    MessageBox.Show("Add Date is not valid"); 
    return; 
} 

和如果可能的話,使用DatePicker控件用於輸入日期。

1

你可以嘗試演如果它不起作用,做些什麼?

private bool ValidInteger(string value) 
{ 
    int notUsed=0; 
    return Int32.TryParse(value, out notUsed); 
} 

我刪除了函數關鍵字。代碼現在編譯。

+0

此代碼不能編譯。 – 2013-03-14 11:11:41

0

我建議你在你的文本框上使用正確的命名。您的代碼表明,當你保存你的約會,您使用的addressTextBox.Text:

myCustomer.AddDate = Convert.ToDateTime(addressTextBox.Text); 

我想應該是這樣的:

myCustomer.AddDate = DateTime.Parse(dateTextBox.Text,"MM/dd/yyyy"); 

也最好使用DateTimePicker控件,而比用戶選擇日期時的文本框要多。希望這可以幫助!

相關問題