?
Solved

C# web service

Posted on 2006-03-23
2
Medium Priority
?
204 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;
            }
0
Comment
Question by:richardmojo
  • 2
2 Comments
 
LVL 2

Expert Comment

by:zeus40
ID: 16274352
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;
          }
0
 
LVL 2

Accepted Solution

by:
zeus40 earned 1500 total points
ID: 16281431
Any luck with my proposed solution?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Screencast - Getting to Know the Pipeline

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question