-1

I have the following WebMethod that returns a conditional string. Unfortunately the if statement doesn't seem to work. I know that the WebMethod is working because I get the Bla bla bla string but not the icons within the if statement. What am I doing wrong?

[WebMethod]
public static string photos()
{
    StringBuilder photos_sb = new StringBuilder();
    photos_sb.AppendFormat("Bla bla bla bla...");
    db = Database.Open("DefaultConnection"); 
    var HasPhoto = db.Query("SELECT [IDphoto] FROM [photos]");
    if (HasPhoto != null)
    {
        photos_sb.AppendFormat("<img src=\"icon-Green.png\" />");
    }
    else
    {
        photos_sb.AppendFormat("<img src=\"icon-Gray.png\" />");
    }
    db.Close();
    db.Dispose();
    photos_sb.AppendFormat("Bla2 bla2 bla2 bla2...");

    return photos_sb.ToString();
}
Gloria
  • 1,305
  • 5
  • 22
  • 57
  • 1
    You forgot to close the strings with a `"`: `" – Rotem Jan 15 '15 at 09:14
  • Have you tried to debug it? Set a breakpoint on the if statement and inspect the **HasPhoto** variable. Are you sure HasPhoto is ever going to be null? – Live Jan 15 '15 at 09:15
  • It is closed in my original code. I forgot to close it while typing my question so this is not the problem. Thank u – Gloria Jan 15 '15 at 09:16
  • var HasPhoto = db.Query("SELECT [IDphoto] FROM [photos]"); Are you getting a value for HasPhoto? – Luca Jan 15 '15 at 09:16
  • @Gloria Post your actual code then. – Rotem Jan 15 '15 at 09:17
  • You aren't closing the `` tags. Also, why are you using `AppendFormat()` and not `Append()` or `AppendLine()`? – DGibbs Jan 15 '15 at 09:22
  • why don't u just debug your function see if "if" statement is working with breaking point – fc123 Jan 15 '15 at 09:24
  • Sorry my img tag is closed in my original code. I've edited my question above. Still the if statementy doesn't return any img.... – Gloria Jan 15 '15 at 09:27
  • Yes HasPhoto can be null and doesn't return any row........ – Gloria Jan 15 '15 at 09:28
  • Which is better and faster in my case Append, AppendFormat or AppendLine? – Gloria Jan 15 '15 at 09:30
  • @Gloria: in terms of performance they aren't likely to be noticeably different. They have different uses though. `Append` just appends the string. `AppendFormat` is used when you have a string with placeholders and you want to then pass a set of objects to put into those placeholders. If you have no placeholders (as here) then there is no point using it. It will likely be slower than `Append` but not significantly. AppendLine will add a newline onto the end of your string. Its not a choice of which is faster but which is the appropriate one to use. – Chris Jan 15 '15 at 09:47
  • If you want more detail ask a question about their usage. Or check if one already exists. Or go read the docs on msdn. – Chris Jan 15 '15 at 09:48
  • 1
    It is impossible that neither branch of you `if` is executing, either `HasPhoto` is null or its not, in either case one branch must execute. Step through the code to determine what is wrong, nobody here can answer this question. – Jamiec Jan 15 '15 at 11:43

2 Answers2

2

Your img tags are missing their closing >. This means that quite possibly they are being outputted but they are not coming out in the format you expected and thus rendering what you expect. The code given will return something like:

Bla bla bla bla...<img src="icon-Gray.png"Bla2 bla2 bla2 bla2...

I would expect a browser to get very confused by this.

If this is in fact a typo and your outputted string really is just "Bla bla bla bla..." then your only possibility would seem to be that you are running the wrong version of your code. IF the above was compiled then it cannot return your value without going through the if statement and running at least one of the branches.

I'd suggest three things:

  1. Ensure you are running the correct compiled code.
  2. Run through it with a debugger to trace its actual execution.
  3. Confirm exactly what your output string is from this method, not what you are seeing several layers of code down the line in whatever is consuming this method.
Chris
  • 27,210
  • 6
  • 71
  • 92
0

I think the answer has been provided already so this is just some helpful advice. Save yourself hassle and reduce repeating code:

photos_sb.Append("Bla bla bla bla...");
[....]
photos_sb.Append("<img src=\"");
if (HasPhoto != null)
{
    photos_sb.Append("icon-Green.png");
}
else
{
    photos_sb.Append("icon-Gray.png");
}
photos_sb.Append("\"" />");

This way your condition is only changing the one bit of code that actually varies.

A slightly cleaner way using AppendFormat would be something like this...

photos_sb.AppendFormat("Bla bla bla bla...");
[....]

string photo = "icon-Gray.png";
if (HasPhoto != null)
{
    photo = "icon-Green.png";
}
photos_sb.AppendFormat("<img src=\"{0}\" />", photo);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
Hugo Yates
  • 2,081
  • 2
  • 26
  • 24