Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends..

I have 6 textboxes for and a search button.
here i want a logic that if any one value entered then only that button method should fire.
else it should throw an error .
provide a bettor solution..
Posted
Comments
Herman<T>.Instance 5-Dec-12 4:13am    
what have you tried? Where do you get stuck?
narla.venkatesh 5-Dec-12 4:18am    
near the if condition. hw can we take 6 statements?
bbirajdar 5-Dec-12 4:18am    
Have you heard of required field validators ? Try them ...
narla.venkatesh 5-Dec-12 4:20am    
actually i have to do server side validation not in the ui
bbirajdar 5-Dec-12 4:22am    
Any reason for increasing the load on server ?

hi dear,
here use one function.

C#
public bool IsValid()
        {
            bool flag = true;

            if (textBox1.Text.Trim() == "")
            {
                ErrPro.SetError(textBox1, "textBox1 value is required.");
                flag = false;
            }
            else
            {
                ErrPro.SetError(textBox1, "");
            }


            if (textBox2.Text.Trim() == "")
            {
                ErrPro.SetError(textBox2, "textBox2 value is required.");
                flag = false;
            }
            else
            {
                ErrPro.SetError(textBox2, "");
            }

            if (textBox3.Text.Trim() == "")
            {
                ErrPro.SetError(textBox3, "textBox3 value is required.");
                flag = false;
            }
            else
            {
                ErrPro.SetError(textBox3, "");
            }

            if (textBox4.Text.Trim() == "")
            {
                ErrPro.SetError(textBox4, "textBox4 value is required.");
                flag = false;
            }
            else
            {
                ErrPro.SetError(textBox4, "");
            }

            if (textBox5.Text.Trim() == "")
            {
                ErrPro.SetError(textBox5, "textBox5 value is required.");
                flag = false;
            }
            else
            {
                ErrPro.SetError(textBox5, "");
            }

            if (textBox6.SelectedIndex == 0)
            {
                ErrPro.SetError(textBox6, "textBox6 value is required.");
                flag = false;
            }
            else
            {
                ErrPro.SetError(textBox6, "");
            }

            return flag;
        }



//-------------------------------------

C#
private void SearchButton_Click(object sender, EventArgs e)
        {
            if (!IsValid() == true)
            {
                return;
            }

            else
            {
              // Continue.
            }
        }
 
Share this answer
 
Here I'v taken only 3 Textboxes.. Make changes as per your need.. Write code under Button_Click event
C#
if (txtCity.Text == "" || txtCountry.Text == "" || txtState.Text=="")
            {
                throw new Exception("Enter data");
            }
            else
            {
                //perform your desire operations
            }
 
Share this answer
 
Comments
bbirajdar 5-Dec-12 4:22am    
Correct one +5
[no name] 5-Dec-12 4:23am    
Thanks man :)
Herman<T>.Instance 5-Dec-12 5:18am    
you could also check via if(string.IsNullOrEmpty(txtCity.text.....)
DaveyM69 5-Dec-12 7:37am    
Rather than == "" it would be better to use String.IsNullOrEmpty(textBox.Text).
Also, exceptions should be more specific - ArgumentNullException would be a better choice here.
[no name] 5-Dec-12 7:39am    
Ya, its a basic idea.. so chill :)

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