简体   繁体   English

在插入数据库之前检查文本框中的空值

[英]Checking for null values on text box before inserting to database

I have a windows form application. 我有一个Windows窗体应用程序。 in which there is one form, which has about 20+ textbox's, i want to verify weather the values are null or not. 其中有一种形式,大约有20多个文本框,我想验证天气值是否为空。 if values are null than error provider shoud point to which textbox is the culprit. 如果值是null,则错误提供者应指出文本框是罪魁祸首。 else store the data to the database. 否则将数据存储到数据库。

I have created a function called "null2" and i'm calling it button click, just before executing the endexecutequery method. 我已经创建了一个名为“ null2”的函数,并且在执行endexecutequery方法之前,我将其称为按钮单击。

here is my code: 这是我的代码:

    public bool null2 (Control control)
    {

        foreach (Control C in control.Controls)
        {
            if (C is TextBox)
            {
                TextBox textbox = C as TextBox;
                if (string.IsNullOrWhiteSpace(textbox.Text))
                {

                    return(false) ;
                }
                else
                {
                    MessageBox.Show("Not happening bro...");
                    errorProvider1.SetError(textbox, "Cannot be Empty");
                    return(false) ;
                }
            }
            else
            {
               return (false);
            }
        }
        return(true);

    }

on button click here 在按钮上单击此处

if (!null2(this))
          {
             // MessageBox.Show("Some empty values are present");
              try
              {
                  int resul = subcom.ExecuteNonQuery();
                  if (resul > 0)
                  {
                      MessageBox.Show("Entered Successfully");
                  }
              }
              catch
              {
                  MessageBox.Show("Some details missing");
              }
         }
         else
        {
            MessageBox.Show("Some empty values are present");
         }

Probably you have your checks wrong. 可能您的支票有误。 If the control text is empty (the text property of a TextBox is never null) you should emit your warning, set the error provider and return false exiting from the loop. 如果控件文本为空(TextBox的text属性从不为null),则应发出警告,设置错误提供程序并从循环中返回false。 Otherwise you end the loop (meaning no TextBox is empty) and return true 否则,您将结束循环(这意味着没有TextBox为空)并返回true

public bool null2 (Control control) 
{
    foreach (TextBox t in control.Controls.OfType<TextBox>())
    {
        if (string.IsNullOrWhiteSpace(t.Text))
        {
            MessageBox.Show("Not happening bro...");
            errorProvider1.SetError(t, "Cannot be Empty");
            return(false) ;
        }
    }
    return(true);
}

Also notice how using IEnumerable.OfType will simplify your code a lot 还要注意,使用IEnumerable.OfType将如何简化您的代码

However, this code doesn't attempt to store anything in the database, so the real problem is in the code that calls this function and how that code handles the return as false from here 但是,此代码不会尝试在数据库中存储任何内容,因此真正的问题在于调用此函数的代码以及该代码如何从此处将其作为false处理返回。

EDIT Change the code in your comment below to this 编辑将下面的注释中的代码更改为此

if (null2(this)) 
{ 
   // If null2 returns true, then execute the query, 
   // there is no need to add another message box for the 
   // empty condition, you have already said that in the null2 method
   try 
   { 
       int resul = subcom.ExecuteNonQuery(); 
       if (resul > 0) 
       { 
           MessageBox.Show("Entered Successfully"); 
       } 
   } 
   catch (Exception ex)
   { 
       MessageBox.Show(ex.Message); 
   }
}

please add the code in your comment below to your original question above 请在下面的注释中将代码添加到上面的原始问题中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM