Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6221381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:07:25+00:00 2026-05-24T08:07:25+00:00

I am trying to read an XML returned by a query, I tested the

  • 0

I am trying to read an XML returned by a query, I tested the query and it is returning the XML with the correct items.. but the code behind has a problem..It reads the first item correctly but then it throws the exception.. I am not sure what is wrong (The exception happens the second time it attempts to go through the WHILE loop. See code below

 // Create Instance of Connection and Command Object
        SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
        SqlCommand myCommand = new SqlCommand("GetItemsXML", myConnection);

        // Mark the Command as a SP
        myCommand.CommandType = CommandType.StoredProcedure;

        // Add Parameters to SP
        SqlParameter parameterPortalID = new SqlParameter("@TheID", SqlDbType.Int, 4);
        parameterPortalID.Value = portalID;
        myCommand.Parameters.Add(parameterPortalID);

        SqlParameter parameterKeywords = new SqlParameter("@myWords", SqlDbType.VarChar, 4000);
        parameterKeywords.Value = myWords;
        myCommand.Parameters.Add(parameterWords);



        // Execute the command
        myConnection.Open();
        SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

        string xmlOut = "";

        while (reader.Read()) //SECOND ITERATION THROWS (Invalid attempt to read
                              // when reader is closed)
        {
            xmlOut = xmlOut + reader[0].ToString();
        }

        xmlOut = "<MyItemsList>" + xmlOut + "</ZMyItemsList>";

        reader.Close();
        myConnection.Close();

        return xmlOut;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:07:26+00:00Added an answer on May 24, 2026 at 8:07 am

    It may have something to do with the fact that the first ittereation would be reader[0] but surely the second itteration will be reader[1], therefore you want to use

     reader.GetString(0)
    

    instead of

     reader[0].ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read xml fields (with actionscript 3), but the problem is that
I'm trying to read a xml file but the ajax code I have keeps
I am trying to read the following xml stream but am really struggling. <channelSnapshot
I have a really simple XML file that I'm trying to read, but I
I'm trying to read an xml document but the XmlReader.ReadToNextSibling does not work as
Im currently trying to read an XML file and populate an observable collection, but
I m trying read XML data using XML parser from Url( https://....etc ). But
I am trying to read an xml feed that has '-' in the element
Hi i'm trying to read xml duration to a new duration but i't says
I'm trying to read a XML file and output the values, but i'm getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.