We help IT Professionals succeed at work.

C# web service

richardmojo
richardmojo asked
on
Medium Priority
217 Views
Last Modified: 2010-04-17
This method never returns that a book is in the collection when it actually is. Can some one please tell me where I'm going wrong.

            /**
             * A method that returns the details of a book from the catalogue
             *
             * @param isbn, the ISBN of the book whose details are desired
             */
            [WebMethod]
            public string isbnQuery(string isbn)
            {
                  string str ="";
                  catalogue = new XmlTextReader(Server.MapPath("Catalogue.xml"));
            
                  while(catalogue.Read( ))
                  {                      
                        if( catalogue.Name == BOOK)
                        {
//                              if( catalogue.GetAttribute("isbn").Equals(isbn))
                              if( catalogue.GetAttribute("isbn") == isbn)
                              {
                                    str = "Title =" + catalogue.GetAttribute("title")+ ", ";
                                    str += "Author =" + catalogue.GetAttribute("author") +", ";
                                    str += "Genre =" + catalogue.GetAttribute("genre");
                              }
                              else
                              {
                                    str = "Book not found";
                              }
                        }                        
                  }//while
                  catalogue.Close();
                  return str;
            }
Comment
Watch Question

Commented:
You are not exiting out of the loop when you find the book....

In it's current state, you are essentially getting this in a loop:

Book not found
Book not found
Title = Title, Author = Author, Genre = Genre
Book not found
Book not found   <-Last entry is returned

And of course Book not found is the last value placed in str

When you find the ISBN == ISBN, then just return str
Example (I am not a C# programmer, but I can understand what you have here)

**
           * A method that returns the details of a book from the catalogue
           *
           * @param isbn, the ISBN of the book whose details are desired
           */
          [WebMethod]
          public string isbnQuery(string isbn)
          {
               string str ="";
               catalogue = new XmlTextReader(Server.MapPath("Catalogue.xml"));
         
               while(catalogue.Read( ))
               {                      
                    if( catalogue.Name == BOOK)
                    {
//                         if( catalogue.GetAttribute("isbn").Equals(isbn))
                         if( catalogue.GetAttribute("isbn") == isbn)
                         {
                              str = "Title =" + catalogue.GetAttribute("title")+ ", ";
                              str += "Author =" + catalogue.GetAttribute("author") +", ";
                              str += "Genre =" + catalogue.GetAttribute("genre");
                              catalogue.Close();
                              return str;

                         }
                         else
                         {
                              str = "Book not found";
                         }
                    }                    
               }//while
               catalogue.Close();
               return str;
          }
Commented:
Any luck with my proposed solution?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.