Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello friends,
this is my code
C#
private void button1_Click(object sender, EventArgs e)
       {
           if (textBox1.Text=="")
           {
               MessageBox.Show("please enter the school Id");
           }
           else
           {
              // int m = Convert.ToInt32(textBox1.Text);
               sb.SCHOOLID = int.Parse(textBox1.Text);
           }
           if (textBox2.Text == "")
           {
               MessageBox.Show("please ente the school name");
           }
           else
           {
               sb.SCHOOLNAME = textBox2.Text.ToString();
           }
           sb.CREATEDDATEANDTIME = DateTime.Now;

when executing this code if condition is chcking when i left the textbox as an empty.What could be the problem?
Posted
Comments
bbirajdar 25-Jul-12 5:09am    
Both the solutions are valid..Why did you downvote them ?

Use the following code, this will help you:

C#
private void button1_Click(object sender, EventArgs e)
{
           if (textBox1.Text.Trim() == String.Empty)
           {
               MessageBox.Show("please enter the school Id");
           }
           else
           {
              // int m = Convert.ToInt32(textBox1.Text);
               sb.SCHOOLID = int.Parse(textBox1.Text);
           }
           if (textBox2.Text.Trim() == String.Empty)
           {
               MessageBox.Show("please ente the school name");
           }
           else
           {
               sb.SCHOOLNAME = textBox2.Text.ToString();
           }
           sb.CREATEDDATEANDTIME = DateTime.Now;
}
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 25-Jul-12 5:16am    
Good suggestion to use Trim().
My 5!
BillW33 25-Jul-12 10:16am    
Trim is a good idea, +5.
Well, if there is nothing in your textbox (not even a space) then if condition will be true and MessageBox will be displayed.
 
Share this answer
 
Comments
baskaran chellasamy 25-Jul-12 4:22am    
i leave the textbox as an empty but when i trace the code if condition is executed and it went to else condition.i dont know why.please help me.
Sandeep Mewara 25-Jul-12 5:28am    
Use Trim() to confirm that it is empty textbox.

use string.IsNullOrEmpty() method for comparison.
Sandeep Mewara 25-Jul-12 5:29am    
Have you asked the real question on why I am not getting message alert, i would have suggested you of using Trim and IsNullOrEmpty methods. Next time, be clear on what are you trying and stuck up with.
bbirajdar 25-Jul-12 5:10am    
You need to Trim() the textbox values as shown in solution 2
bbirajdar 25-Jul-12 5:11am    
countered ..+5
C#
private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("please enter the school Id");
                return;
            }
            else if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("please ente the school name");
                return;
            }
            else
            {
                // int m = Convert.ToInt32(textBox1.Text);
                sb.SCHOOLID =  Convert.ToInt32(textBox1.Text);
                sb.SCHOOLNAME = textBox2.Text.ToString();
                sb.CREATEDDATEANDTIME = DateTime.Now;
            }
        }
 
Share this answer
 
v2
Comments
bbirajdar 25-Jul-12 5:10am    
countered ..+5
Raghunatha_Reddy_S 26-Jul-12 1:16am    
:-)
Prasad_Kulkarni 25-Jul-12 5:16am    
Good alternative +5
Raghunatha_Reddy_S 26-Jul-12 1:16am    
:-)
BillW33 25-Jul-12 12:18pm    
Good answer, +5.

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