空文本框仍然保存SQL Server中的数据,即使表中的列不允许为空

本文关键字:不允许 数据 保存 SQL Server 文本 | 更新日期: 2023-09-27 18:04:10

我面临这个问题,当我点击保存按钮时,所有的文本框都是空的,它显示所有的星星标签。当我填充最后一个文本框留下所有其他空,它保存数据到数据库与空字符串。

我该如何处理这个问题?

if (tbIDCardNum.Text.Trim() == "")
{
    lblStarIDCardNum.Visibility = Visibility.Visible;
}
if (tbFirstName.Text.Trim() == "")
{
    lblStarFirstName.Visibility = Visibility.Visible;
}
if (rbMale.IsChecked == false && rbFemale.IsChecked == false)
{
    lblStarGender.Visibility = Visibility.Visible;
}
if (tbDOB.Text == "")
{
    lblStarDOB.Visibility = Visibility.Visible;
}
if (tbDateOfJoining.Text == "")
{
    lblStarDOJ.Visibility = Visibility.Visible;
}
if (tbEducation.Text.Trim() == "")
{
    lblStarEducation.Visibility = Visibility.Visible;
}
if (tbCNIC.Text.Trim() == "")
{
    lblStarCNIC.Visibility = Visibility.Visible;
}
if (tbSalary.Text.Trim() == "")
{
    lblStarSalary.Visibility = Visibility.Visible;
}
if (tbAddress.Text.Trim() == "")
{
    lblStarAddress.Visibility = Visibility.Visible;
}
if (tbEmail.Text.Trim() == "")
{
    lblStarEmail.Visibility = Visibility.Visible;
}
if (tbContact1.Text.Trim() == "")
{
    lblStarContact.Visibility = Visibility.Visible;
}
else
{
    try
    {
        conn.Open();
        cmd.CommandText = "insert into teacher (tIDCardNum, tFirstName, tLastName,tGender, tDOB, tCNIC, tEducation, tSalary, tJoinedOn, tAddress, tEmail, tContact1, tContact2, tContact3,tStatus) values ('" + tbIDCardNum.Text.Trim() + "' , '" + tbFirstName.Text.Trim() + "' , '" + tbLastName.Text.Trim() + "' , '" + gender + "' , '" + tbDOB.Text + "', '" + tbCNIC.Text + "' , '" + tbEducation.Text + "' , '" + tbSalary.Text.Trim() + "' , '" + tbDateOfJoining.Text.Trim() + "' , '" + tbAddress.Text.Trim() + "', '" + tbEmail.Text + "' , '" + tbContact1.Text + "' , '" + tbContact2.Text + "' , '" + tbContact3.Text + "',1)";
        cmd.Connection = conn;
        cmd.ExecuteNonQuery();
        cmd.Clone();
        conn.Close();
        HideStars();
        Refresh();
        MessageBox.Show("Saved");
   }
   catch (Exception ex)
   {
       if (ex.Message.Contains("Violation of PRIMARY KEY constraint "))
       {
           conn.Close();
           MessageBox.Show(ex.Message);
       }
       else
       {
           MessageBox.Show(ex.Message);
       }
   }
}

空文本框仍然保存SQL Server中的数据,即使表中的列不允许为空

正如我所理解的那样,似乎主要问题是您检查不同if语句中的所有字段,但是只有最后一个有else。正如我从你的职位假设,这是你的问题;你希望每个文本框在插入数据库之前都有值,对吧?

如果把你的代码分解成一些更容易重用的东西,这是更好的解释,顺便也清理了一些东西。

首先,在你的类中引入一个变量,我们可以用它来查看是否有任何空字段:

private bool HasEmptyFields = false;

接下来,让我们创建一个简单的助手,检查文本框是否为空/null,更新相应标签的可见性状态,如果确实为空,则将'HasEmptyFields'设置为true:

private void ValidateField(TextBox textBox, Label label) {
    // check if the textbox actually is null - or empty (""), which is a difference
    // the nifty helper string.IsNullOrEmpty() will help with that
    var fieldIsEmpty = string.IsNullOrEmpty(textBox.Text.Trim());
    // next, based on if the field is empty,  set the visibility of the label
    // don't worry, this is fancy syntax for a simple if...then...else
    label.Visibility = fieldIsEmpty ? Visibility.Visible : Visibility.Hidden;
    if (fieldIsEmpty) {
        // ONLY if this field is actually null, or empty, we make sure to 
        // inform the rest of the code this occ
        HasEmptyFields = true;
    }
}

设置好后,我们可以这样做:

ValidateField(tbIDCardNum, lblStarIDCardNum);
ValidateField(tbFirstName, lblStarFirstName);
// etc... continue doing this for all you fields
if (HasEmptyFields) {
    // ONLY if there is any field detected as being empty/null
    // we simply stop here (and skip the insert-into-db stuff)
    return;
} 
try 
{
    // if all fields indeed have a value, let's
    // continue with the insert-into-db stuff here
    conn.Open();
    ...
} 

现在肯定有办法让它更漂亮。但这可能会帮助你找到正确的方向。还值得一提的是其他一些注释,比如防止SQL注入(这是必然会发生的),以及研究数据验证工具,这样您就不必自己编写所有这些验证代码。但这显然不在本文的讨论范围之内。

如果您想要求填写字段,那么您真的应该使用必填字段验证器-参见https://msdn.microsoft.com/en-us/library/5hbw267h%28VS.80%29.aspx?f=255&MSPPError=-2147217396