Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
                       }







                        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();




                       }
                   }
               }






           }
Posted
Comments
Richard C Bishop 14-Jan-13 17:26pm    
Are you getting any exceptions?
zeshanazam 14-Jan-13 17:31pm    
query is not working.. no results, nothing happening, no output. no error..i think if else conditions are creating problems.
Richard C Bishop 14-Jan-13 17:37pm    
Ok, one thing I do notice is that your "Account_No" field is being passed a text value. If the database is expecting an int, then it won't work. Check the datatype for that field in the database and if it is an int, then convert or parse the cboaccount.text to an int. You will also have to remove the '' from it as it is not a string value. Another thing, after your "Account_No" field, you have a dash instead of a tick mark, that needs to be changed too. Also you need and "AND" between each condition in your where clause.
joshrduncan2012 14-Jan-13 17:27pm    
Please let us know what is wrong. We can't help you unless we know the reason why your code isn't functioning properly.
zeshanazam 14-Jan-13 17:31pm    
query is not working.. no results, nothing happening, no output. no error..i think if else conditions are creating problems.

Can you give this a shot? If it still doesnt run youll need to set break points in your app and step through it.

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

if ((cbotitle.Text != "" ||cboname.Text!="") && !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();
	return;
}

if ((cbotitle.Text != "" ||cboname.Text!="") && !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();
	
	return;
}

if (!Regex.Match(cboaccount2.Text, @"^[0-9]{1}$").Success)
{
	MessageBox.Show("Invalid Account No", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
	cboaccount2.SelectAll();
	cboaccount2.Focus();
	
	return;
}

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

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


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();

}


If you have issues stepping through it let me know
 
Share this answer
 
Comments
zeshanazam 14-Jan-13 18:26pm    
what will return do here ??
David_Wimbley 14-Jan-13 18:27pm    
The nested if else's were beginning to get me all confused. Return will stop execution of the method if it goes inside the if statements...essentially doing what your nested if else statements were doing...just less confusing. There are better ways to go about this im sure but this was the quickest way i could clean it up for my understanding.

So if it meets the if statements conditions it will stop execution and not run your sql query. If it doesnt meet (successful) your if conditions then it should...in theory...reach the sql query and execute providing it passes all of your validation.
zeshanazam 14-Jan-13 18:31pm    
same problem :(
David_Wimbley 14-Jan-13 18:32pm    
Can you set break points and see what if statement it is going into and stopping execution? The problem is not with the code anymore (except for maybe your regex's) but rather its probably with what data you are inputting in through the UI (winforms?)
zeshanazam 14-Jan-13 18:35pm    
it is not fully checking if statements but executing query.
This code has been tested, i refactored the code even more for cleanliness and instead of using regex used linq/c# for some of your validation.

You will need to make sure you are using the following using

C#
using System.Linq;


C#
bool cboAcctNullOrEmpty = string.IsNullOrEmpty(cboaccount1.Text);
bool cboAcct2NullOrEmpty = string.IsNullOrEmpty(cboaccount2.Text);
bool cboTitleNullOrEmpty = string.IsNullOrEmpty(cbotitle.Text);
bool cboNameNullOrEmpty = string.IsNullOrEmpty(cboname.Text);

bool cboAcctIsDigit = cboaccount1.Text.All(Char.IsDigit);
bool cboAcct2IsDigit = cboaccount2.Text.All(Char.IsDigit);
bool cboNameIsLetter = cboname.Text.All(Char.IsLetter);
bool cboTitleIsLetter = cbotitle.Text.All(Char.IsLetter);

if (cboAcctNullOrEmpty || cboAcct2NullOrEmpty || cboTitleNullOrEmpty || cboNameNullOrEmpty)
{
     MessageBox.Show("Plz Provide Some Field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     cboaccount1.Focus();
     return;
}
//After this point you dont need to test for empty strings as any textbox that has an empty string will not get to this point, it will be caught in the above if statement and the return will stop code execution.

if (cboaccount1.Text.Length != 4 || !cboAcctIsDigit)
{
     MessageBox.Show("Invalid Account NO\nShould be of type e.g 8459-5", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     cboaccount1.SelectAll();
     cboaccount1.Focus();
     return;
}

if (cboaccount1.Text.Length != 4 || !cboAcctIsDigit)
{
     MessageBox.Show("Invalid Account NO\nShould be of type e.g 8459-5", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     cboaccount1.SelectAll();
     cboaccount1.Focus();
     return;
}

if (cboaccount2.Text.Length != 1 || !cboAcct2IsDigit)
{
     MessageBox.Show("Invalid Account No", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     cboaccount2.SelectAll();
     cboaccount2.Focus();
     return;
}

if (!cboNameIsLetter)
{
     MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     cboname.SelectAll();
     cboname.Focus();
     return;
}


if (!cboTitleIsLetter)
{
     MessageBox.Show("Invalid title", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     cbotitle.SelectAll();
     cbotitle.Focus();
     return;
}

MessageBox.Show("Run SQL Based Code Here");
 
Share this answer
 
v4
Comments
zeshanazam 14-Jan-13 23:14pm    
THANKS :)
C#
public void cond()
       {
           Account_Search acse = new Account_Search();
           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 == "")
               {


                   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\nShould be of type e.g 8459-5", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       cboaccount2.SelectAll();
                       cboaccount2.Focus();
                   }
                   else
                       btn_search();
               }





                else
                {

                    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();
                        }
                        else
                        {
                            btn_search();
                        }


                    }


                    else
                    {
                        if (cboaccount1.Text == "" && cboaccount2.Text == "" && cboname.Text == "")
                        {
                            cboaccount1.Enabled = true;
                            cboaccount2.Enabled = true;
                            cboname.Enabled = true;
                            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
                            {
                                btn_search();
                            }

                            }


                    }


           }
           }
 
Share this answer
 

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