locked
Update Drop Down List automatically after insert/update/delete RRS feed

  • Question

  • User-2055741253 posted

    Hello,

    I am trying to automatically update/refresh my year and month drop down list when a new data is add or when data is deleted/updated. I am trying to do this by adding DropDownList.DataBind(); method. It does update the new information but it also duplicates the existing data. Can someone please help understand what do I need to do differently here?

    Insert Command:

    var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT (METRIC_YEAR, METRIC_MONTH, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT) VALUES  (:YEAR, :MONTH_NUM, :MONTH_TEXT, :TAB_NAME, :SECTION_NAME, :COMMENT_ORDER, :COMMENT_TEXT)");
                OracleCommand command = new OracleCommand(insertQuery);
    
                //ADD PARAMETERS FOR INSERT STATEMENT
                command.Parameters.Add(new OracleParameter("YEAR", drpDownYear.Text));
                command.Parameters.Add(new OracleParameter("MONTH_NUM", month));
                command.Parameters.Add(new OracleParameter("MONTH_TEXT", drpDownMonth.Text));
                command.Parameters.Add(new OracleParameter("TAB_NAME", drpDownTabName.Text));
                command.Parameters.Add(new OracleParameter("SECTION_NAME", drpDownSectionName.Text));
                command.Parameters.Add(new OracleParameter("COMMENT_ORDER", drpDownCommentOrder.Text));
                command.Parameters.Add(new OracleParameter("COMMENT_TEXT", txtComment.Text));
    
                Utility.Exec(command);
                RefreshGrid();
                DropDownMonth.DataBind();
                DropDownYear.DataBind();

     

    Update Command:

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                if (Page.IsValid)
                {
                    Label id = GridView1.Rows[e.RowIndex].FindControl("txtID") as Label;
                    TextBox comment = GridView1.Rows[e.RowIndex].FindControl("txtEditComment") as TextBox;
                    TextBox order = GridView1.Rows[e.RowIndex].FindControl("txtCommentOrder") as TextBox;
                    var updateQuery = ("UPDATE CHNGMETRICS.VW_BRD_COMMENT SET COMMENT_ORDER =  :COMMENTORDER , COMMENT_TEXT = :COMMENTTEXT WHERE BRD_COMMENT_ID = :BRDCOMMENTID ");
                    OracleCommand command = new OracleCommand(updateQuery);
                    
                    //Add PARAMETERS FOR UPDATE STATEMENT
                    command.Parameters.Add(new OracleParameter("COMMENTORDER", order.Text));
                    command.Parameters.Add(new OracleParameter("COMMENTTEXT", comment.Text));
                    command.Parameters.Add(new OracleParameter("BRDCOMMENTID", Convert.ToInt32(id.Text)));
                    GridView1.EditIndex = -1;
                    Utility.Exec(command);
                    RefreshGrid();
                    DropDownYear.DataBind();
                    DropDownMonth.DataBind();

    Monday, August 7, 2017 1:05 AM

Answers

  • User2103319870 posted

    It does update the new information but it also duplicates the existing data.

    Try clearing the items in dropdownlist before binding data to dropdownlist

    var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT (METRIC_YEAR, METRIC_MONTH, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT) VALUES  (:YEAR, :MONTH_NUM, :MONTH_TEXT, :TAB_NAME, :SECTION_NAME, :COMMENT_ORDER, :COMMENT_TEXT)");
    			OracleCommand command = new OracleCommand(insertQuery);
    
    			//ADD PARAMETERS FOR INSERT STATEMENT
    			command.Parameters.Add(new OracleParameter("YEAR", drpDownYear.Text));
    			command.Parameters.Add(new OracleParameter("MONTH_NUM", month));
    			command.Parameters.Add(new OracleParameter("MONTH_TEXT", drpDownMonth.Text));
    			command.Parameters.Add(new OracleParameter("TAB_NAME", drpDownTabName.Text));
    			command.Parameters.Add(new OracleParameter("SECTION_NAME", drpDownSectionName.Text));
    			command.Parameters.Add(new OracleParameter("COMMENT_ORDER", drpDownCommentOrder.Text));
    			command.Parameters.Add(new OracleParameter("COMMENT_TEXT", txtComment.Text));
    
    			Utility.Exec(command);
    			RefreshGrid();
    			//Clear the items from dropdownlist before binding with updated data
    			DropDownMonth.Items.Clear();
    			DropDownMonth.DataBind();
    			DropDownYear.Items.Clear();
    			DropDownYear.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 1:53 AM

All replies

  • User2103319870 posted

    It does update the new information but it also duplicates the existing data.

    Try clearing the items in dropdownlist before binding data to dropdownlist

    var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT (METRIC_YEAR, METRIC_MONTH, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT) VALUES  (:YEAR, :MONTH_NUM, :MONTH_TEXT, :TAB_NAME, :SECTION_NAME, :COMMENT_ORDER, :COMMENT_TEXT)");
    			OracleCommand command = new OracleCommand(insertQuery);
    
    			//ADD PARAMETERS FOR INSERT STATEMENT
    			command.Parameters.Add(new OracleParameter("YEAR", drpDownYear.Text));
    			command.Parameters.Add(new OracleParameter("MONTH_NUM", month));
    			command.Parameters.Add(new OracleParameter("MONTH_TEXT", drpDownMonth.Text));
    			command.Parameters.Add(new OracleParameter("TAB_NAME", drpDownTabName.Text));
    			command.Parameters.Add(new OracleParameter("SECTION_NAME", drpDownSectionName.Text));
    			command.Parameters.Add(new OracleParameter("COMMENT_ORDER", drpDownCommentOrder.Text));
    			command.Parameters.Add(new OracleParameter("COMMENT_TEXT", txtComment.Text));
    
    			Utility.Exec(command);
    			RefreshGrid();
    			//Clear the items from dropdownlist before binding with updated data
    			DropDownMonth.Items.Clear();
    			DropDownMonth.DataBind();
    			DropDownYear.Items.Clear();
    			DropDownYear.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 7, 2017 1:53 AM
  • User-2055741253 posted

    This does the trick but everytime a new comment is added or deleted, The year and month drop down are automatically get defaulted to January and most recent year.

    Monday, August 7, 2017 1:34 PM
  • User2103319870 posted

    The year and month drop down are automatically get defaulted to January and most recent year.

    Consider setting the selected item in dropdownlist after calling databind like below

    var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT (METRIC_YEAR, METRIC_MONTH, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT) VALUES  (:YEAR, :MONTH_NUM, :MONTH_TEXT, :TAB_NAME, :SECTION_NAME, :COMMENT_ORDER, :COMMENT_TEXT)");
                OracleCommand command = new OracleCommand(insertQuery);
    
                //ADD PARAMETERS FOR INSERT STATEMENT
                command.Parameters.Add(new OracleParameter("YEAR", drpDownYear.Text));
                command.Parameters.Add(new OracleParameter("MONTH_NUM", month));
                command.Parameters.Add(new OracleParameter("MONTH_TEXT", drpDownMonth.Text));
                command.Parameters.Add(new OracleParameter("TAB_NAME", drpDownTabName.Text));
                command.Parameters.Add(new OracleParameter("SECTION_NAME", drpDownSectionName.Text));
                command.Parameters.Add(new OracleParameter("COMMENT_ORDER", drpDownCommentOrder.Text));
                command.Parameters.Add(new OracleParameter("COMMENT_TEXT", txtComment.Text));
    
                Utility.Exec(command);
                RefreshGrid();
                //Clear the items from dropdownlist before binding with updated data
                DropDownMonth.Items.Clear();
                DropDownMonth.DataBind();
                //Set the selecteditem to dropdownlist after databinding
                DropDownMonth.SelectedIndex = DropDownMonth.Items.IndexOf(DropDownMonth.Items.FindByText("Change the value here to selected item value"));
                DropDownYear.Items.Clear();
                DropDownYear.DataBind();
                DropDownYear.SelectedIndex = DropDownYear.Items.IndexOf(DropDownYear.Items.FindByText("Change the value here to selected item value"));

    Monday, August 7, 2017 1:52 PM
  • User-2055741253 posted

    I want the values in the drop down to stay the same what user has selected regardless of adding new data or deleting data. Would you suggest to take a different approach to auto refresh drop down list? based on my research online AJAX allows this functionality but I am not familiar with AJAX and don't know how to implement that code. Please help.  

    Monday, August 7, 2017 8:10 PM