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.