我需要检查用户是否填写了表格中的所有文本框,即使其中一个是空白,我也需要向用户发送一条错误消息,例如“请填写详细信息”。
我需要确保用户填写了所有文本框。
private void Button1_Click(object sender, EventArgs e)
{
String isbn = txtIsbn.Text;
String name = txtName.Text;
String author = txtAuthor.Text;
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\Documents\Library.mdf;Integrated Security=True;Connect Timeout=30");
string query = "insert into Book(ISBN,bookName,bookAuthor) values('" + isbn + "','" + name + "','" + author + "')";
SqlCommand cmd = new SqlCommand(query, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Records Added.");
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
con.Close();
}