0

I have some code below:

SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.Parameters.AddWithValue("@company", txtCompany.Text);
command.Parameters.AddWithValue("@server", txtServer.Text);
command.Parameters.AddWithValue("@username", txtUserName.Text);
command.Parameters.AddWithValue("@password", txtPassword.Text);

How do i validate empty textboxes making sure that the textboxes are alway populated?

I have tried:

if (string.IsNullOrEmpty(txtCompany.Text))

{
      //do work here
}

else
{

}

However im not sure how i can assign ALL text boxes like this? in a cleaner way which limits the number of lines of code i have to write?

8
  • Couldn't get your question! What exactly you want to do? Commented May 18, 2014 at 9:31
  • I need to stop my code from running if there is "" in all the text boxes. Commented May 18, 2014 at 9:31
  • possible duplicate of Loop through Textboxes Commented May 18, 2014 at 9:32
  • Stopping code from running if textbox is empty! Means you want to terminate application on the fly when there is no data in textboxes. I feel it strange. Commented May 18, 2014 at 9:33
  • 1
    @PriceCheaperton: It's a question about how to access a series of text boxes to perform the same operation on each of them without copy-and-pasting the same code for each text box. At least, your final paragraph sure sounds like that. Commented May 18, 2014 at 9:34

2 Answers 2

2
    private bool ValidateTextBoxes()
    {
        try
        {
            string textBoxData = string.Empty;

            foreach (Control item in this.Controls)
            {
                if (item.GetType() == typeof(TextBox))
                {
                    textBoxData += item.Text;
                }
            }
            return (textBoxData != string.Empty);
        }
        catch { return false; }
    }

    if(ValidateTextBoxes())
    {
         // your code..
    }

Just call the ValidateTextBoxes method before performing database operations e.g

Sign up to request clarification or add additional context in comments.

Comments

0

Handle the TextBox_Validating for all textBoxes:

public Form1()
{
    InitializeComponent();

    txtCompany.Validating += TextBox_Validating;
    txtServer.Validating  += TextBox_Validating;
    txtUserName.Validating  += TextBox_Validating;
    txtPassword.Validating  += TextBox_Validating;
}
private void TextBox_Validating(object sender, CancelEventArgs e)
{
    TextBox control = sender as TextBox;
    control.Focus();   
    e.Cancel = control.Text == string.Empty;
}

Also you can add this code before executing the command:

bool controlsAreValid = true;

foreach (Control control in this.Control)
{
   if (control is TextBox)
   {
      if (control.Text == string.Empty)
      {
          controlsAreValid = false;
          break;
      }
   }
}
if (!controlsAreValid)
     return;

9 Comments

@jahanviKashyap Yes, I know, But I do not understand the downvote!
i was wondered too. I dint this downvote. you can see i dont even have enough repo to down vote. plus i don't down vote for such awesome answers.
Just adding this code will not prevent the query to be executed with empty strings. Show how you can use validation to prevent the query from being executed. Something with ValidateChildren().
Looping over all controls and checking whether they're a textbox that contain text does not leverage the Validating event. All you need is if (ValidateChildren()) { DoQuery(); }.
@CodeCaster But you can put them in a groupBox and loop over groupBox.Controls
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.