我目前正在从事一个学术图书馆系统项目(我是一个非常基本的C#用户),并且我正在尝试实施验证,当前仅使用客户名称进行测试,并使用了错误提供程序工具。< / p>
我已经实现了验证,其中一个字段应仅接受字母/数字/等(Regex.IsMatch),但是现在我想实现验证,其中,在单击“保存客户按钮”时,如果“名称”文本框为EMPTY / NULL,在生成/保存新的客户记录之前,将生成一条警告,提示需要提交名称字段(必填)。目前,我已经尝试执行此操作,但是出现了一些错误。
请注意,在创建新客户(提交所有必填字段时)后,新记录将插入到我与Visual Studio连接的本地数据库中。请参阅下面的代码以供参考。
P.S。我知道我需要努力使用3层/ MVC结构的实现。
namespace Library_System_Project
{
public partial class AddCustomerRecordForm : Form
{
public AddCustomerRecordForm()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
// START Validation
Customer myCustomer = new Customer();
String Name;
String Surname;
DateTime DateofBirth;
String Address;
int TelephoneNumber;
int MobileNumber;
Boolean quit = true;
do
{
Name = this.txtName.Text;
if (Name != null)
{
if (Name == "")
{
MessageBox.Show("Customer's name is required. Please do not leave this field empty.");
this.erp_Provider.SetError(this.txtName, "Customer's name is required. Kindly submit the details in the given field.");
quit = true;
break;
}
else
{
quit = false;
this.erp_Provider.Dispose();
}
}
else
{
return;
}
//ABOVE - END Validation
//BELOW - START DB Connection / Insert upon Save
string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename= C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf ;Integrated Security=True;Connect Timeout=30";
string Query = "insert into Customers (CustomerName, CustomerSurname, CustomerDateofBirth, CustomerAddress, CustomerTelephoneNumber, CustomerMobileNumber) values ('" + this.txtName.Text.Trim() + "','" + this.txtSurname.Text.Trim() + "','" + this.msktxtDateofBirth.Text.Trim() + "','" + this.txtAddress.Text.Trim() + "','" + this.txtTelephoneNumber.Text.Trim() + "','" + this.txtMobileNumber.Text.Trim() + "');";
SqlConnection DBCon = new SqlConnection(ConnectionString);
SqlCommand DBCommand = new SqlCommand(Query, DBCon);
SqlDataReader DBReader;
try
{
DBCon.Open();
DBReader = DBCommand.ExecuteReader();
MessageBox.Show("New customer record added to the system.", "Library System", MessageBoxButtons.OK);
while (DBReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:0)
我已经稍微整理了一下代码,并做了一些修改,认为对您有帮助。
namespace Library_System_Project
{
public partial class AddCustomerRecordForm : Form
{
public AddCustomerRecordForm()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
// START Validation
Customer myCustomer = new Customer();
String Name;
String Surname;
DateTime DateofBirth;
String Address;
// *** You may want these to be strings ***
int TelephoneNumber;
int MobileNumber;
Name = this.txtName.Text;
if (string.IsNullOrWhiteSpace(Name))
{
MessageBox.Show("Customer's name is required. Please do not leave this field empty.");
// *** Where is this declared ? ***
// *** I've declared an instance in a using scope so it is disposed of automatically ***
using(var erp_provider = new ErrorProvider())
erp_Provider.SetError(this.txtName, "Customer's name is required. Kindly submit the details in the given field.");
// *** Return if validation fails ***
return;
}
//ABOVE - END Validation
//BELOW - START DB Connection / Insert upon Save
string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename= C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf ;Integrated Security=True;Connect Timeout=30";
string Query = "insert into Customers (CustomerName, CustomerSurname, CustomerDateofBirth, CustomerAddress, CustomerTelephoneNumber, CustomerMobileNumber) values ('" + this.txtName.Text.Trim() + "','" + this.txtSurname.Text.Trim() + "','" + this.msktxtDateofBirth.Text.Trim() + "','" + this.txtAddress.Text.Trim() + "','" + this.txtTelephoneNumber.Text.Trim() + "','" + this.txtMobileNumber.Text.Trim() + "');";
SqlConnection DBCon = new SqlConnection(ConnectionString);
SqlCommand DBCommand = new SqlCommand(Query, DBCon);
SqlDataReader DBReader;
try
{
DBCon.Open();
DBReader = DBCommand.ExecuteReader();
MessageBox.Show("New customer record added to the system.", "Library System", MessageBoxButtons.OK);
while (DBReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}finally
{
// *** If you're going to be opening a connection be sure to close it ***
// *** Finally blocks work well for this ***
DBCon.Close()
}
}
}
}