Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am new to the Sharepoint 2010 development, and have a requirement where in the Visual Web part i have a dropdown and this dropdown needs to be popluated with the Column values of another list.
Being precise, the Dropdownlist_projectName should be populated from the Name Column of a list named "Project". I was trying to work with the below piece of code but i don't know where i am going wrong-
C#
SPQuery ratecardQuery = new SPQuery();

            ratecardQuery.ViewFields = "<FieldRef Name='Project_x0020_Name'/>";
            ratecardQuery.ViewFieldsOnly = true;
            ratecardQuery.Query = "<Where><Eq><FieldRef Name=\"Project Name\"/><Value Type=\"Text\"></Value></Eq></Where>";

            SPList resourceRateList = SPContext.Current.Web.Lists["Project"];
            SPListItemCollection item = resourceRateList.GetItems(ratecardQuery);

            DataTable tmpTable = new System.Data.DataTable();
            tmpTable = item.GetDataTable();


When i debug the code, i see tmpTable coming as null.
Please help.
Posted

1 solution

I dont know why the community dint gave any response but here is the solution-
C#
SPQuery query = new SPQuery();
               query.ViewFieldsOnly = true;
               query.ViewFields = "<fieldref name="Project_x0020_Name" />";
               using (SPSite site = new SPSite(SPContext.Current.Web.Url))
               {
                   using (SPWeb web = site.OpenWeb())
                   {
                       SPList list = web.Lists["Project"];
                       if (list != null)
                       {
                           DataTable dt = new DataTable();
                           dt = list.GetItems(query).GetDataTable();
                           DropDownList1.DataSource = dt;
                           DropDownList1.DataValueField = "Project_x0020_Name";
                           DropDownList1.DataTextField = "Project_x0020_Name";
                           DropDownList1.DataBind();
                           DropDownList1.Items.Insert(0, "--Select Project Name--");

                       }
                   }

               }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900