Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
                                        <Where>
                                            <Or>
                                                <Eq><FieldRef Name='Status' /><Value Type='Choice'>New</Value></Eq>
                                                <Eq><FieldRef Name='Status' /><Value Type='Choice'>Published</Value></Eq>
                                            </Or>
                                        </Where>
                                        <OrderBy>
                                            <FieldRef Name='Created' Ascending='False' />
                                        </OrderBy>
                                       </Query>";
                    SPListItemCollection itemCollection = mainList.GetItems(ourQuery);
                    foreach (SPListItem item in itemCollection)
                    {
                        DropDownList1.Items.Add(item);
                    }
                }
            }       
        }
    }

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.