我正在尝试编写一个简单的 GUI 应用程序,当单击检索按钮时,它将从 Customer 类加载客户数据。然后,当单击保存按钮时,此数据将存储在 Customer 对象中。
我通过测试发现我的清除和检索按钮按预期工作,但是当我按下保存按钮时收到以下错误消息:
您的应用程序中发生了未处理的异常。如果单击继续,应用程序将忽略此错误并尝试继续。如果单击退出,应用程序将立即关闭。
该字符串未被识别为有效的日期时间。从索引 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("");
}
}
}