SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I don't know why i only get one row as per below: When they are 2 rows to be returned. Am i missing something using SpQuery? Thanks in Advance

string siteUrl = "http://mysite:8080/sites/Intranet";
            SPWeb _web = new SPSite(siteUrl).OpenWeb("Commus");

            var items = _web.Lists["Read"].GetItems(GetQuery());

            foreach (SPListItem item in items)

                this.Label1.Text = item["Title"].ToString() + "-" + item["user"].ToString();


      private SPQuery GetQuery()
            SPQuery _query = new SPQuery();

            string username = "myusername";

            _query.Query = @"<Query><Where>
                             <And><Eq><FieldRef Name='username'/><Value Type='Text'>" + username + "</Value></Eq></And></Where></Query>";

            _query.ViewFields = @" <FieldRef Name='Title' />
                                     <FieldRef Name='username' />";

            return _query;
share|improve this question
up vote 2 down vote accepted

Are you for sure that only one row is being returned? When you are iterating through the listitems that are returned, you are setting the Label1.Text each time to that record. Using the code above, you would only ever see the last record returned.

Try something like:

this.Label1.Text += string.Format("{0}-{1}<br />", item["Title"].ToString(), item["user"].ToString());
share|improve this answer
thanks CNutt.Forgot to iterate.Also I removed the <Query> element and the <And> Cheers – naijacoder Jan 31 '12 at 6:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.