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 == "" && 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 14-Jan-13 11:21am
Comments
richcb at 14-Jan-13 17:26pm
   
Are you getting any exceptions?
zeshanazam at 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.
richcb at 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 at 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 at 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.
David_Wimbley at 14-Jan-13 17:37pm
   
From the sounds of it your saying your query is not working correct? We dont have access to your database so we cant test/correct your query, i dont see anything blatantly wrong with your sql so its more then likely your data your searching for.
zeshanazam at 14-Jan-13 17:39pm
   
it is not reaching to query block
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();

 


}
}
jibesh at 14-Jan-13 18:42pm
   
Ok...you may comment or skip the checking conditions for now and start profile your code as below
 
1. Just execute the SQL part of the code and see your query working, for test trial you ignore the where condition
2. i.e check select * from account returns any data if yes go to step 3
3. include the where condition in your query, a hard coded where condition is good enough to understand your where query also working fine.
4. If step 2 and step 3 works fine without error do include the validations
5. put a break point start of this method and try to debug your application to know what is happening in real time
6. Do read the Article link I provided in my comment for how to debug.
zeshanazam at 14-Jan-13 19:03pm
   
Query is working fine.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Can you give this a shot? If it still doesnt run youll need to set break points in your app and step through it.
 
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
  Permalink  
Comments
zeshanazam at 14-Jan-13 18:26pm
   
what will return do here ??
David_Wimbley at 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 at 14-Jan-13 18:31pm
   
same problem :(
David_Wimbley at 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 at 14-Jan-13 18:35pm
   
it is not fully checking if statements but executing query.
David_Wimbley at 14-Jan-13 18:38pm
   
What does "not fully checking if statements" mean? I agree i think you've got to understand the basics before you go any further, if you cant tell me what if statement it is "not fully checking" and what your input is then i dont think i can be of anymore help.
 
You aren't getting an exception correct? Just not executing code?
 
I recommend now looking at your regex that you've got and make sure they are correct. You can use this regex tester to run some tests on what your using. http://regexpal.com/
jibesh at 14-Jan-13 18:34pm
   
If you dont know how to debug your application spend some time reading this article
David_Wimbley at 14-Jan-13 18:36pm
   
I agree with this fully
zeshanazam at 14-Jan-13 18:37pm
   
ok guys, but what now with if statements... ???
David_Wimbley at 14-Jan-13 18:39pm
   
like i said before the issue is not with the code, with the exception of your regex's, its more than likely an issue with the regex that you are using or the data you are inputting.
zeshanazam at 14-Jan-13 18:42pm
   
what is issue with regex ??
David_Wimbley at 14-Jan-13 18:43pm
   
That its probably wrong? I dont know i dont quite understand what your code is doing...what your end goal is...im not involved in your project. So you tell me what each regex line is suppose to be doing and then maybe i can help some.
 
Right now it looks like all your doing is using your regex to test length when it would be much easier to just do textbox1.Text.Length instead of a regex.
zeshanazam at 14-Jan-13 18:45pm
   
!Regex.Match(cbotitle.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
if input value of combo box cbotitle will not be alphabets then error message
MessageBox.Show("Invalid title", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
should appear
David_Wimbley at 14-Jan-13 18:47pm
   
So are you saying its entering the fifth if statement that i provided in my code or are you talking about your code?
David_Wimbley at 14-Jan-13 18:51pm
   
Can you give me what you are putting into the following textboxes.
 
cboaccount1.Text
cboaccount2.Text
cbotitle.Text
cboname.Text
 
If you do that ill put the code into my test app and see whats going on.
David_Wimbley at 14-Jan-13 18:44pm
   
You need to also provide what data you are putting into your text box's so that way your "issue" can be replicated.
zeshanazam at 14-Jan-13 18:54pm
   
not just fifth if, all if not checking regex conditions.....i m entering numbers instead of alphabets in these fieldds.
David_Wimbley at 14-Jan-13 19:06pm
   
wait, so your saying your entering in only numbers for regexs where your testing for only letters. Of course its going to fail
 
if cbotitle.Text is all numbers it will fail this regex evaluation !Regex.Match(cbotitle.Text, @"^[A-Za-z][A-Za-z. ]*$").Success)
 
As my earlier request stated, can you give me the exact values for each one of these textbox fields. I know it seems trivial but it may just actually find the solution faster.
 
cboaccount1.Text
cboaccount2.Text
cbotitle.Text
cboname.Text
 
zeshanazam at 14-Jan-13 18:55pm
   
i m using your code..
David_Wimbley at 14-Jan-13 18:59pm
   
are you able to use System.Linq namespace?
zeshanazam at 14-Jan-13 19:02pm
   
yup using this...
jibesh at 14-Jan-13 18:32pm
   
return just skip the following sql operation. you need to understand the more about programming basics before you go further thats my sincere advice.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
using System.Linq;
 
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");
  Permalink  
v4
Comments
zeshanazam at 14-Jan-13 23:14pm
   
THANKS :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

                    }
 

           }
           }
  Permalink  

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

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 265
2 ClimerChinna 222
3 vikinghunter 188
4 OriginalGriff 161
0 Sergey Alexandrovich Kryukov 8,363
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | 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