John Deck
John Deck

Reputation: 899

using if statement to compare a combo box value with two text boxes value

I'm using C# winforms I have a form with combobox called cmbExport and two textboxes txtDateSend and txtSendNum The combobox get its data from a stored procedure GET_ALL_EXPORT_WITHNULL

cmbExport.DataSource = cmp.GET_ALL_EXPORT_WITHNULL(); 
cmbExport.DisplayMember = "side";   
cmbExport.ValueMember = "ID_EXPORT";  
cmbExport.SelectedValue = "6";

When the user NOT choose certain values from the combobox and one of the textboxes are empty a meesage box appear

I tried this code but it didn't work:

int x = Convert.ToInt32(cmbExport.SelectedValue); //Its already integer but the code didn't accept int x = cmbExport.SelectedValue; ???

string ds = txtDateSend.Text;
string sn = txtSendNum.Text;

if ((x != 6 || x != 42 || x != 1042) && string.IsNullOrEmpty(sn))
    {
       MessageBox.Show("you should enter a send number");
       return;
    }
       else if ((x != 6 || x != 42 || x != 1042) && string.IsNullOrEmpty(ds))
    {
       MessageBox.Show("you should enter a date send);
       return;
    } 

thank you

Upvotes: 2

Views: 1739

Answers (3)

Mohammad Al Baghdadi
Mohammad Al Baghdadi

Reputation: 377

You can't use int x = cmbExport.SelectedValue because mbExport.SelectedValue return String

Upvotes: 0

dmyoko
dmyoko

Reputation: 647

You can improve the intention revealing of your code by creating a list, for example:

var dependsOnSendNumber = new [] {6, 42, 1042};

And then simply use a Linq query:

if (dependsOnSendNumber.Contains(x) && string.IsNullOrEmpty(sn))

This improves readability, and you can make the dependsOnSendNumber list to be created dynamically accordingly to some rule. So, if anytime a new option is created that follows the same rule, the only thing you need to do is to set it accordingly to be included in the list.

Upvotes: 2

Mureinik
Mureinik

Reputation: 312136

Since no number can be both 6 and 42, every number is different from either 6 or 42, so your if statement always evaluates to true. I think you meant to use && instead of || there:

if (x != 6 && x != 42 && x != 1042 && string.IsNullOrEmpty(sn)

Upvotes: 2

Related Questions