0

好吧,我有一个简单的程序正在写入数据库。我正在尝试像这样向文本框添加验证,

private void textBox1_TextChanged(object sender, EventArgs e)
{
    try
    {
         if (textBox1.Text.Length < -1)
         {
             MessageBox.Show("Don't Leave this field blank!");
         }
    }
    catch
    {
         //todo
    }
} 

和我保存到数据库代码,

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Server = DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;");
    SqlCommand sql1 = new SqlCommand("INSERT into Book VALUES('" + textBox1.Text + "' , '" + textBox2.Text + "','" + dateTimePicker1.Value.ToShortDateString() + "')", con);
     con.Open();
     sql1.ExecuteNonQuery();
     con.Close();
     this.bookTableAdapter.Fill(this.booksDataSet.Book);
     MessageBox.Show("Data Added!");
     this.Close();    
}

但它仍然在向数据库中添加空白数据,奇怪的是在数据库中我不允许为空,但仍在添加数据。任何线索我错了?

4

3 回答 3

2

我没有看到您停止数据库以添加空内容。您正在验证 textbox_textchanged 事件,该事件只会在有人输入数据时验证文本。您需要对 button1 的单击事件进行验证,如下所示:

    private void button1_Click(object sender, EventArgs e)
    {
        if(string.IsNullOrEmpty(textBox1.Text.Trim()))
        {
            MessageBox.Show("Null String !!!!!!!!");
            return;
        }
        SqlConnection con = new SqlConnection("Server = DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;");
        SqlCommand sql1 = new SqlCommand("INSERT into Book VALUES('" + textBox1.Text + "' , '" + textBox2.Text + "','" + dateTimePicker1.Value.ToShortDateString() + "')", con);
        con.Open();
        sql1.ExecuteNonQuery();
        con.Close();
        this.bookTableAdapter.Fill(this.booksDataSet.Book);
        MessageBox.Show("Data Added!");
        this.Close();

    }
于 2012-08-11T21:15:01.397 回答
0

一个空的 textbox.Text 的字节长度为零,而不是 -1。
为确保文本框不为空,您应该将 Trim() 应用于当前文本
此外,无需在此代码上捕获异常。

此外,不要使用 TextChanged 事件来验证文本框。为此目的有验证事件。(记得为控件设置 CauseValidation=True)

private void textBox1_Validating(object sender, CancelEventArgs e)
{
        if (textBox1.Text.Trim().Length == 0) 
        { 
            MessageBox.Show("Don't Leave this field blank!"); 
             e.Cancel = true; 
        } 
}  

当然,您可以将代码中的所有验证移到您更新数据库的位置

private void button1_Click(object sender, EventArgs e)      
{   
    if (textBox1.Text.Trim().Length == 0) 
    { 
        MessageBox.Show("Don't Leave this field blank!"); 
        return;
    } 

    using(SqlConnection con = new SqlConnection(
          "Server=DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;");      
    {
        SqlCommand sql1 = new SqlCommand("INSERT into Book " + 
                           "VALUES(@text1, @text2,@dtValue", con);      
        sql1.Parameters.AddWithValue("@text1", textBox1.Text);
        sql1.Parameters.AddWithValue("@text2", textBox2.Text);
        sql1.Parameters.AddWithValue("@dtValue", dateTimePicker1.Value.ToShortDateString());
        con.Open();      
        sql1.ExecuteNonQuery();      
        this.bookTableAdapter.Fill(this.booksDataSet.Book);      
        MessageBox.Show("Data Added!");      
        this.Close();      

    }      
}
于 2012-08-11T21:08:18.467 回答
0
private void button1_Click(object sender, EventArgs e)
{
    if(string.IsNullOrEmpty(textBox1.Text) || textBox1.Text.Trim().Length == 0)
    {
        //log
        return;
    }

    SqlConnection con = new SqlConnection("Server = DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;");
    SqlCommand sql1 = new SqlCommand("INSERT into Book VALUES('" + textBox1.Text + "' , '" + textBox2.Text + "','" + dateTimePicker1.Value.ToShortDateString() + "')", con);
    con.Open();
    sql1.ExecuteNonQuery();
    con.Close();
    this.bookTableAdapter.Fill(this.booksDataSet.Book);
    MessageBox.Show("Data Added!");
    this.Close();
}
于 2012-08-11T21:28:13.977 回答