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;

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());
|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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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