尽管TextBox验证,数据仍然会被添加

时间:2012-08-11 21:04:53

标签: c# c#-4.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();    
}

但它仍然是在数据库中添加空白数据而奇怪的是在数据库中我没有允许null但仍然添加了数据。我错的任何线索?

3 个答案:

答案 0 :(得分: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();

    }

答案 1 :(得分:0)

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

此外,请勿使用TextChanged事件来验证文本框。 为此目的有Validating事件。 (请记住为控件设置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();      

    }      
}

答案 2 :(得分: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();
}