Click here to Skip to main content
15,913,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (TextBox1.Text == null)
        {
        Label1.Text = "Input Valid values";
        }
        else {
            sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
            sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;

            sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr2).InnerText;
            sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr2).InnerText;

            Label1.Text = sLong1 + ", " + sLat1;
            Label2.Text = sLong2 + ", " + sLat2;


        }


the concept of this is
i want to check if the textbox value is null or not

i put a if condition like this

but its not working...........


when i keep text box in null value
it will not go to the if condition

straightly point to the else code and point to the slong1 value and gives error saying

Object reference not set to an instance of an object.
Posted

There are various approaches you can do to this,

personally I would go with one of the following:

If the .Net framework version you are using is less than version 4
C#
if(String.IsNullOrEmpty(TextBox1.Text.Trim())

else go with:
C#
if(String.IsNullOrWhiteSpace(TextBox1.Text ))

The latter approach does not require you to trim the string first as it will detect if the string is just whitespace characters such as space, carriage returns etc.
 
Share this answer
 
Try this..... :)

C#
if (TextBox1.Text == "")
        {
        Label1.Text = "Input Valid values";
        }
        else {
            sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
            sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;
 
            sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr2).InnerText;
            sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr2).InnerText;
 
            Label1.Text = sLong1 + ", " + sLat1;
            Label2.Text = sLong2 + ", " + sLat2;
 

        }
 
Share this answer
 
Comments
Thanks7872 15-Jul-13 8:44am    
What if i give single white space?Does it enter into if?
Nirav Prabtani 15-Jul-13 8:47am    
yes obvious...:)
bunzitop 15-Jul-13 8:55am    
are you sure?? if i white space is given then it enters to if condition!!
Nirav Prabtani 15-Jul-13 9:00am    
yes if you give white space then it will going through condition...:)
are you sure about that???
bunzitop 15-Jul-13 9:21am    
ya i tried it in widows form, but didn't work :O
if you want to check if there is only white spaces then you can do

if (TextBox1.Text.Trim() == "")
        {
        Label1.Text = "Input Valid values";
        }
        else {
            sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
            sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;
 
            sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr2).InnerText;
            sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr2).InnerText;
 
            Label1.Text = sLong1 + ", " + sLat1;
            Label2.Text = sLong2 + ", " + sLat2;
 
 
        }
 
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