Search code examples
c#linqtextboxcontrolcollection

How to filter empty textboxes with LINQ?


I have 6 textboxes in my panel but not all will be filled in on each user submission. How can I use this LINQ method but filter out the empty textboxes?

   public void LogHB()
      {
          var myTextBoxes = HashboardPanel.Controls
          .OfType<TextBox>()
          .Where;
          foreach(TextBox txt in myTextBoxes)
          {
             string hbserial = txt.Text;          
             try
             {
                  SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HMT2DBCS"].ConnectionString);
                  using (SqlCommand command = new SqlCommand("spHBcheckin", connection))
                  {
                       command.CommandType = CommandType.StoredProcedure;
                       command.Parameters.AddWithValue("@order_id", txtorder_id.Text);
                       command.Parameters.AddWithValue("@hbserial", hbserial);
                  }
              }
          }
      }

Solution

  • var myTextBoxes = HashboardPanel.Controls
        .OfType<TextBox>()
        .Where(tb => !string.IsNullOrWhitespace(tb.Text));