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

时间:2015-07-22 18:24:37

标签: c# sql-server database if-statement null

我正面临这个问题,当我在所有文本框都为空时单击“保存”按钮时,它会在所有文本框上显示星标。当我填写最后一个文本框而其他所有文本框都为空时,它会将数据保存到空字符串数据库中

我该如何处理这个问题?

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);
       }
   }
}

2 个答案:

答案 0 :(得分:0)

正如我所理解的那样,主要问题是您检查不同if语句中的所有字段,但只有最后一个字段有。正如我从你的帖子中假设的那样,这是你的问题;你希望每个文本框都有一个值,然后才开始在数据库中插入它,对吗?

如果将代码分解为更可重用的东西,偶然清理一些东西,可以更好地解释。

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

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 injection(必然会发生),以及查看数据验证工具,这样您就不必自己编写所有这些验证代码。但显然,这不在这个答案的范围内。

答案 1 :(得分:0)

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