Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
if (cboaccount1.Text != "" || cboaccount2.Text != "" ||cbotitle.Text != "")
               {
                   if (!Regex.Match(cboname.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
                       MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       cboname.SelectAll();
                       cboname.Focus();
                   }
Posted 14-Jan-13 10:37am
Comments
David_Wimbley at 14-Jan-13 16:39pm
   
your question is not clear and your code snippet doesnt even resemble what...to me...i think your title does. Please use the Improve Question widget to make your question more clear.
zeshanazam at 14-Jan-13 16:42pm
   
i want to check first
if (cboaccount1.Text != "" || cboaccount2.Text != "" ||cbotitle.Text != "")
if it is not true then
if (!Regex.Match(cboname.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cboname.SelectAll();
cboname.Focus();
}
 
but both working at a same time..
David_Wimbley at 14-Jan-13 16:48pm
   
Is this what you need then?
 
if (cboaccount1.Text != "" || cboaccount2.Text != "" || cbotitle.Text != "")
{

}
else
{
if (!Regex.Match(cboname.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
{
MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
 
cboname.SelectAll();
cboname.Focus();
}
zeshanazam at 14-Jan-13 16:52pm
   
oh sorry i want to check
if
if (cboaccount1.Text != "" || cboaccount2.Text != "" ||cbotitle.Text != "")
it is true then
if (!Regex.Match(cboname.Text, @"^[A-Za-z][A-Za-z. ]*$").Success) MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); cboname.SelectAll(); cboname.Focus(); }
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to nest your if statement for the second portion. It should look like this:
 

if (cboaccount1.Text != "" || cboaccount2.Text != "" || cbotitle.Text != "")
               {
                 if(!Regex.Match(cboname.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
                 {
 MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       cboname.SelectAll();
                       cboname.Focus();
                 }
              }
  Permalink  
v4
Comments
zeshanazam at 14-Jan-13 17:11pm
   
what is wrong with this ??
 

if (cboaccount1.Text == "" && cboaccount2.Text == "" && cbotitle.Text == "" && cboname.Text == "")
{
MessageBox.Show("Plz Provide Some Field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
cboaccount1.Focus();
 
}
else

if (cbotitle.Text != "" ||cboname.Text!="")
{
}
else if (!Regex.Match(cboaccount1.Text, @"^[0-9]{4}$").Success)
{
MessageBox.Show("Invalid Account NO\nShould be of type e.g 8459-5", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cboaccount1.SelectAll();
cboaccount1.Focus();
}
else if (!Regex.Match(cboaccount2.Text, @"^[0-9]{1}$").Success)
{
MessageBox.Show("Invalid Account No", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cboaccount2.SelectAll();
cboaccount2.Focus();
}

else
{
if (cboaccount1.Text != "" || cboaccount2.Text != "" || cbotitle.Text != "")
{
}
 
else if (!Regex.Match(cboname.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
{
MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cboname.SelectAll();
cboname.Focus();
}
else
{
if (cboaccount1.Text != "" || cboaccount2.Text != "" || cboname.Text != "")
{
}
else if (!Regex.Match(cbotitle.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
{
MessageBox.Show("Invalid title", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cbotitle.SelectAll();
cbotitle.Focus();
}
 
else
{
 

SqlCommand cmd = new SqlCommand("Select * from Accounts where Account_No='" + cboaccount1.Text + "-" + cboaccount2.Text + "'or Account_title= '" + cbotitle.Text + "'or Name='" + cboname.Text + "'", cn);
cmd.CommandType = CommandType.Text;
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
 
if (ds.Tables[0].Rows.Count == 0)
{
MessageBox.Show("No Record Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
acse.dataGridView1.DataSource = ds.Tables["ss"];
this.Hide();
acse.ShowDialog();
this.Close();
}
}
 






}
}
richcb at 14-Jan-13 17:24pm
   
Can you add that code using the "Improve Question" widget please? I cannot read that in the comment box.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here is exactly the difference between '&' and '&&', '|' and '||'.
 
The operator using double characters "optimizes" the check: the expression is evaluated left-to-righ. The evaluation is performed only until it makes sense, from the standpoint of final Boolean value. Further evaluation is not performed when the result of it is already known from already complete evaluation steps. In particular, in the sequence of '&&' operators, when some intermediate result if false, further evaluation is not performed, because none of next members can possibly change the result to true, because false & true is always false anyway. Likewise, in the sequence of '||' operators, when some intermediate result if true, further evaluation is not performed, because none of next members can possibly change the result to false, because true | false is always true, anyway.
 
Isn't that clear?
 
Now, it's can possibly still give a wrong execution, not in the Boolean result, but in side-effect of some function, when some members of Boolean expressions are Boolean function with some side effect. This way, some calls to those function can be "optimized out", so the side effects will be skipped.
 
One approach to resolve this would be using '|' or '&'. Another, generally better approach would be avoiding using Boolean functions with side effects, at least in Boolean expressions. A number of computer languages does not allow such function at the level of compilation.
 
Now, armed with this understanding, you can get full control over execution of your Boolean expression. This is all you want to know. Of course, sometime you also may prefer using (nested) if blocks, where everything is explicit.
 
—SA
  Permalink  
v3

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



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100