Click here to Skip to main content
15,909,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to get my method to return more than one query result. the current method, is only outputting 1 record. Please advise - Thank you

What I have tried:

SQL
SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        string title = reader.GetString(0);  
                        string body = reader.GetString(4);

                        string pub = reader["publication_id"].ToString();
                        string iss = reader["issue_id"].ToString();
                        string sid = reader["STORYID"].ToString(); 


                        string c = url(title, pub, iss, sid);

            DateTime dt = DateTime.Today;

            XElement xeRoot = new XElement("article");

            XElement xeStatus = new XElement("status", "Approved");
            xeRoot.Add(xeStatus);

            XElement xeTitle = new XElement("title", title);
            xeRoot.Add(xeTitle);

            XElement xeSubTitle = new XElement("subtitle", title);
            xeRoot.Add(xeSubTitle);

            XElement xeSynopsis = new XElement("synopsis", body + "...");
            xeRoot.Add(xeSynopsis);

            XElement xeURL = new XElement("url", c);
            xeRoot.Add(xeURL);

            XElement xeDisplayDate = new XElement("display_date", dt);
            xeRoot.Add(xeDisplayDate);

            XDocument xDoc = new XDocument(xeRoot);
            return xDoc.ToString();

                    }
                }
            }
            return null;
      
        }
Posted
Updated 17-Jun-16 3:36am
v2
Comments
PIEBALDconsult 17-Jun-16 9:34am    
How do you know it isn't? You'll need places to put them.

1 solution

Well...let's rip some code out of your sample:
C#
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    string title = reader.GetString(0);  
    ...
    XDocument xDoc = new XDocument(xeRoot);
    return xDoc.ToString();

}
You return from the method at the end of the first loop...
 
Share this answer
 
Comments
miss786 17-Jun-16 9:43am    
Hi, Thank you for your suggestion and help. I am little unclear in regards your solution. Should i be returning the method outside the loop? Thanks
OriginalGriff 17-Jun-16 9:57am    
Well it'd help ... as soon as your code meets a return statement, that's it - the method is over. So if your loop ends with return, it will always exit at the end of the first row.
But... it doesn't look like you've thought about that code at all - the code inside the loop doesn't look like it does anything that can be "carried on" to teh next iteration.
It's as if you said:

int i = 0;
while (reader.Read())
{
i = (int) reader["value"];
}

The value of i will always be the value of the last row in the reader, because it overwrites the value each time it gores round the loop.

Now, I have no idea what you are trying to do, or how it fits into the rest of your app - but if you want to process multiple rows, you need to do something other than "overwrite each time" as you are at the moment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900