Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Hi,
I am completely new to c# .so i am just into basics.Just now i am beginning to work with windows forms.Now i have to add a 2 text boxes and 1 insert button now when i'm entering the textboxes it should be inserted when the button is clicked.Now what i need is that when the text boxes is empty and when i clicked the button , it should show a error.I Think this is very small problem. But as i'm new, pl guide me to do it .
 
Regards
Balamurugan
Posted 20-Nov-12 20:00pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Lets say you textbox name ID is txtBox. then try this in your submit button code:
 
if(txtBox.Text.Trim() == string.Empty)
{
 Messagebox.Show("Please enter something in the textbox");
 return; // return because we don't want to run normal code of buton click
}
 
// your normal button code will some here
  Permalink  
Comments
islam_ashraful at 14-Sep-13 3:30am
   
Thanks Rahul
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this out,
if (TextBox1.Text == "")
                    MessageBox.Show("Enter Some Text in TextBox1");
  Permalink  
v2
Comments
Balamurugan1989 at 21-Nov-12 1:16am
   
It shows the error i.e "Object Reference not set to an instance of object"
Krunal R at 21-Nov-12 1:18am
   
I have updated the soultion, try that one..
Balamurugan1989 at 21-Nov-12 1:20am
   
I tried ur code only...it is not working...
Krunal R at 21-Nov-12 1:24am
   
Tell me,Where are you writing this code ??
And in my code, its working.. ok Try to put Breakpoint and debug it...
Marcus Kramer at 21-Nov-12 9:58am
   
+5. Correct. (I would suggest using string.Empty instead of "" in the comparison though.)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

For Empty textbox validation we need to follow the below code:
if (textBox1.Text == "")
{
   string myStringVariable1 = string.Empty;
   MessageBox.Show("User Name is requierd");
}
else if (textBox2.Text == "")
{
   string myStringVariable2 = string.Empty;
   MessageBox.Show("Password is requierd");
}
//For combobox validation we should follow the below code.
else if (comboBox1.Text == "--Choose--")
{
   string myStringVariable3 = string.Empty;
   MessageBox.Show("Select User Type");
}
  Permalink  
v2
Comments
Silvabolt at 11-Jul-13 12:21pm
   
This would only show one error at a time. Rather than doing this, you can check all fields at once, save them and notify user all of the errors at once. Furthermore, string.Empty should be used instead of "". Also, all of the solutions including these ones will break if textbox happens to be null. Solution 3 makes a good remark about the 'return', because this is probably handled in an event handler method. I know this is old, but none of these solutions are really complete.

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.1411022.1 | Last Updated 21 Nov 2012
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