Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a C# Winform where I have a textbox I need to verify if the text entered is either in the database table or the textbox is blank. I am using a leave event.

I can verify the entered text in the textbox is in the table.

I can verify if the textbox is empty.

how can I do both?

What I have tried:

code to verify textbox entry is in the database table.
private void BillBCOValue_Leave(object sender, EventArgs e)
   {
      var testresult = GlobalConfig.AccountTable.GetByAccount(BillBCOValue.Text);

      if (testresult == null) 
            
      {
       MessageBox.Show("BCO must be a valid Account Code");
       BillBCOValue.Focus();
      }
Posted
Updated 17-Sep-20 18:27pm

1 solution

If I understood correct, wouldn't that be a simple if:
C#
private void BillBCOValue_Leave(object sender, EventArgs e)
{
  if(string.IsNullOrWhiteSpace(BillBCOValue.Text))
  {
     MessageBox.Show("Textbox empty!");
  }
  else
  {
      var testresult = GlobalConfig.AccountTable.GetByAccount(BillBCOValue.Text);
    
      if (testresult == null) 
            
      {
         MessageBox.Show("BCO must be a valid Account Code");
         BillBCOValue.Focus();
      }
  } 
}

Refer: String.IsNullOrWhiteSpace(String) Method (System) | Microsoft Docs[^]
 
Share this answer
 
Comments
MIDCOPC 28-Sep-20 19:17pm    
Yes for you it was that simple. I am new to this. Thank you for helping!
Sandeep Mewara 29-Sep-20 0:13am    
welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900