none
Can not get a Dropdown list to populate with data RRS feed

  • Question

  • I am trying to get this section of code to work right

    I am doing C# with a Visual Webpart. If this needs to go the .net area let me know

    but here is my code

    I am using a SharePoint list for data: Repair Improvement

    ddlRepairImprovement is the name of the dropdown

    column name in list:
    Repair_x0020_Improvement

    /// <param name="RepairImprovement"></param>
            private void PopulateRepairImprovement(string RepairImprovement)
            {

                try
                {
                    if (ViewState["dtRepairImprovement"] != null)
                    {
                        DataTable dtRepairImprovement = (DataTable)ViewState["dtRepairImprovement"];

                       DataRow[] rows = dtRepairImprovement.Select("Repair_x0020_Improvement='" + RepairImprovement + "'");
                       
                
                        DataTable dt = dtRepairImprovement.Clone();
                        foreach (DataRow row in rows)
                        {
                            dt.ImportRow(row);
                        }

                        dt.AcceptChanges();

                        ddlRepairImprovement.DataSource = dt;

                        //datatextfield and datavaluefield        
                        ddlRepairImprovement.DataTextField = DataTextField;
                        ddlRepairImprovement.DataValueField = DataValueField;

                        //databind
                        ddlRepairImprovement.DataBind();

                     if (RepairImprovement == null || RepairImprovement == string.Empty || RepairImprovement == NONE || dt.Rows.Count == 0)

                        {
                            ddlRepairImprovement.Items.Insert(0, NONE);
                            lblRepairImprovementValue.Text = NONE;
                        }
                        else
                        {
                            ddlRepairImprovement.SelectedValue = RepairImprovement;
                            lblRepairImprovementValue.Text = RepairImprovement;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //error logging
                    ErrorLogger errorLogger = new ErrorLogger();
                    errorLogger.LogErrorMessages("PopulateRepairImprovement", ex.Message);

                    HandleException(ex);
                }
            }

            /// <summary>

    not sure what is wrong.



    • Edited by cowboy2066 Monday, September 16, 2019 9:52 PM
    Monday, September 16, 2019 9:51 PM

All replies

  • Hi,

    The issue should be related to your DataTable dt = dtRepairImprovement.Clone(), try to debug the dt contains data and ddlRepairImprovement.DataTextField = DataTextField; map to valid column.

    You could try to debug the webpart solution by Visual Studio debug tool(F5).

    My test code for your reference.

    protected void Page_Load(object sender, EventArgs e)
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                 {
                     using (SPWeb web = site.OpenWeb())
                     {
                         SPList list = web.Lists.TryGetList("ttttt");
    
                        DataTable dt = new DataTable();
    
                        dt.Columns.Add("dtRepairImprovement", typeof(string));
                        foreach (SPListItem item in list.Items)
                        {
            dt.Rows.Add(item["Title"].ToString());
                        }
                        ddlRepairImprovement.DataSource = dt;
    
                        ddlRepairImprovement.DataTextField = "dtRepairImprovement";
    
          ddlRepairImprovement.DataValueField = "dtRepairImprovement";
                        ddlRepairImprovement.DataBind();
                    }
                }
            }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, September 18, 2019 2:27 AM
  • the clone() is not the issue.

    I do not want to do it from the pageload. I could not even get the sample code provided to work in the private void as per below.

    I want to do it from the populate class:

    private void PopulateUpgrade(string Upgrade)
            {

                try
                {
                    if (ViewState["dtUpgrade"] != null)
                    {
                        DataTable dtUpgrade = (DataTable)ViewState["dtUpgrade"];

                        DataRow[] rows = dtUpgrade.Select("Upgrade='" + Upgrade);
                        //DataTable dt = dtUpgrade;
                        DataTable dt = new DataTable();
                      
                        foreach (DataRow row in rows)
                        {
                           
                            dt.ImportRow(row);
                        }

                        dt.AcceptChanges();

                        ddlUpgrade.DataSource = dt;

                        //datatextfield and datavaluefield        
                       
                        ddlUpgrade.DataTextField = "dtUpgrade";
                       
                        ddlUpgrade.DataValueField = "dtUpgrade";

                        //databind
                        ddlUpgrade.DataBind();

                 if (Upgrade == null || Upgrade == string.Empty || Upgrade == NONE || dt.Rows.Count == 0)
                       {
                           ddlUpgrade.Items.Insert(0, NONE);
                            lblUpgradeValue.Text = NONE;
                       }
                        else
                        {
                            ddlUpgrade.SelectedValue = Upgrade;
                            lblUpgradeValue.Text = Upgrade;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //error logging
                    ErrorLogger errorLogger = new ErrorLogger();
                    errorLogger.LogErrorMessages("PopulateUpgrade", ex.Message);

                    HandleException(ex);
                }
            }

    this is the page.. you click on the DD.. nothing happens.

    Any ideas???? my code is above.

    the other pages that the code refers are attached as "using" items.





    • Edited by cowboy2066 Friday, September 20, 2019 3:27 PM
    Thursday, September 19, 2019 8:00 PM
  • Hi,

    You could try to debug the webpart solution by Visual Studio debug tool(F5).

    So you could monitor the actual value of your DataTable object(including Data columns by quick watch).

    Here is one thread for your reference.

    https://www.youtube.com/watch?v=sACkw915kmg

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, September 23, 2019 1:06 AM
  • I have done F5.. no luck doesnt say there is an error.
    Monday, September 23, 2019 1:37 AM
  • Hi,

    Try to monitor the DataTable value as I shared in your another thread.

    https://social.msdn.microsoft.com/Forums/office/en-US/c76a11c4-bea3-4ca7-b54b-fe847e94027f/need-help-with-error-code-and-what-it-means-error-method-not-found-systemdatadatatable?forum=sharepointdevelopmentprevious#118bd376-6685-4c54-85ef-70294b808877

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, September 24, 2019 7:33 AM
  • Can you break down what I need to do? As I tried following the sample, and I still do not get the item to debug or find anything wrong

    Still can not populate the Dropdown list.

    I just need something that will get the data from the sharepoint list and NOT be in the PageLoad area

    Ok, I was able to skip out the Page_Load issue. I hve he dropdown working

    I have under the page_load a Populate Upgrade()

    and then under the code behind

    • Edited by cowboy2066 Tuesday, September 24, 2019 11:11 PM
    Tuesday, September 24, 2019 9:55 PM