-2

In C#, if statement I am capturing the length property and want to apply condition statement for that. But i believe my condition statement is not working. How should I apply condition statement for the given code. length "66" means internet connection is down else its up.

WebClient client = new WebClient();
string value;
try
{
    value = client.DownloadString("http://google.com");
}
catch(WebException ex)
{
    value = (ex. Message);
}
if (value = "66")
{
    Console.WriteLine("Internet connection is down");
}
else
{
    Console.WriteLine("Internet connection is up");
}
Console.WriteLine(value.Length);           
Console.WriteLine("Press any key to continue");
Console.ReadKey(true);
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
Sohail
  • 780
  • 3
  • 14
  • 27
  • 7
    You could change the code to `if (value.Length == "66")`. But even then i would assume that it's not a good idea to rely on the length of an error message (consider f.e. localization). – Tim Schmelter Oct 08 '12 at 09:09
  • i just want to internet connection is down so when internet connection is down it always show "Internet explorer cannot display the webpage" this is actually i m check my Load balancer URl. I will get two results only one is empty html string or internet explorer cannot display the page. thanks – Sohail Oct 08 '12 at 09:21

4 Answers4

9

value = "66" is an assignment, not an equality comparison, which would be value == "66".

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

if (value = "66") -> if (value == "66")

Must return a boolean

Roshana
  • 428
  • 3
  • 15
0
if (value = "66")

means, go to the statement, if the assignment of the String "66" to the variable value are successful

Jo need to use "=="

if (value == "66")

= -> assignment == -> equal

Thargor
  • 1,862
  • 14
  • 24
0

I am really not sure this is a safe way to detect internet connectivity. When I try this code and pull my network cable out, I get

"The remote name could not be resolved: 'google.com'"

which is 51 characters.

There doesn't seem to be a property on WebClient to detect connectivity, but this post might help.

Community
  • 1
  • 1
Colin Desmond
  • 4,824
  • 4
  • 46
  • 67