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 have a list containing fields "FirstName" and "Status". The "Status" field is a dropdown list containing "New" and "Published" options. I intend to code a visual webpart that will display the FirstName when the Status is set to New.

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            using (SPSite site = new SPSite(@"http://sharesrv"))
                using (SPWeb website = site.OpenWeb())
                    SPList mainList = website.Lists["Main List"];
                    SPQuery ourQuery = new SPQuery();
                    ourQuery.Query = @"<Query>
                                                <Eq><FieldRef Name='Status' /><Value Type='Choice'>New</Value></Eq>
                                                <Eq><FieldRef Name='Status' /><Value Type='Choice'>Published</Value></Eq>
                                            <FieldRef Name='Created' Ascending='False' />
                    SPListItemCollection itemCollection = mainList.GetItems(ourQuery);
                    foreach (SPListItem item in itemCollection)

I hope the CAML query is correct, if not please correct me. Now I used the DropDownList control to display the "FirstNames" into it, but I am unable to do and I am getting this error.

cannot convert from 'Microsoft.SharePoint.SPListItem' to 'System.Web.UI.WebControls.ListItem'

Please suggest the modifications.

share|improve this question

You need to remove <Query> and </Query> when building ourQuery and change foreach loop to:

foreach (SPListItem item in itemCollection)
    DropDownList1.Items.Add(new ListItem(item.Title));

Everything else looks ok. Try it and report back if you have some more issues.

share|improve this answer
Hello Vedran, I tried the tip that you gave me....dList.Items.Add(new ListItem(item["FirstName"].ToString())); But the dropdown list doesn't display anything. It supposed to display the info from "FirstNames" column. Please suggest. – Chandra Shekhar Oct 6 '11 at 19:43
Did you remove <Query> tag form ourQuery.Query? Can you verify that itemCollection is not empty? – Vedran Rasol Oct 6 '11 at 21:41

If it is webpart, then inherit the proper webpart class. Instead of extending your class with WebPart. You can extend it microsoft.sharepoint.webpartpages.webpart. It will work for sure .. chk it....

share|improve this answer

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.