locked
DropDownList shows only first value and TextBox.Visible=false return exception RRS feed

  • Question

  • User-106287432 posted

    Hello! I have program with  autogenerated gridview. When row is selected, Textboxes and DropDownLists are generated (depend on the column name).
    I have fields with DDL which is working fine. I will explain with example:

    if (DataTable.Columns[i].ToString() == "path_output_pattern" )
                        {
    
                            TxtBox.Visible = false;
                            DropDownList1.Items.Add("yyyyMMdd");
                            DropDownList1.Items.Add("wwww+D+n");
                            DropDownList1.Items.Add("");
                            DropDownList1.DataBind();
                            DropDownList1.Attributes.Add("class", "form-control");
                            DropDownList1.ID = DataTable.Columns[i].ToString();
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(DropDownList1);
                            TxtBox.Text = DropDownList1.SelectedValue;
    
                        }

    Here generated Textbox is hidden and DDL has the following values and it is add in the specific column. When user choose, for example "wwww+D+n" value, Txtbox.Text will be "wwww+D+n".

    So this is fine. But now i want to have 3 DDL in the same cell.

    if (DataTable.Columns[i].ToString() == "start_date_time" || DataTable.Columns[i].ToString() == "end_date_time" )
                        {
                            
                            TxtBox.Visible = false;
                            DBTableGrid.Rows[0].Cells[i + 1].Attributes.Add("style", "white-space: nowrap;");
    
                            days.DataSource = Enumerable.Range(01, 31);
                            days.DataBind();
                            //days.Items.Insert(0, new ListItem("", "0"));
                            days.ID = DataTable.Columns[i].ToString() + "days";
                            days.Attributes.Add("style", "height: 30px;");
    
    
                            months.DataSource = Enumerable.Range(1, 12);
                            months.DataBind();
                            //months.Items.Insert(0, new ListItem("", "0"));
                            months.Attributes.Add("style", "height: 30px;");
                            months.ID = DataTable.Columns[i].ToString() + "months";
    
    
                            //range for years
                            for (int year = 2010; year <= 2040; year++)
                            {
                                years.Items.Add(year.ToString());
                            }
                            years.DataBind();
                            //years.Items.Insert(0, new ListItem("", "0"));
                            years.Attributes.Add("style", "height: 30px;");
                            years.ID = DataTable.Columns[i].ToString() + "years";
    
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(days);
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(months);
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(years);
    
                   
    
                            TxtBox.Text = DateTime.Parse(days.SelectedValue + "/" + months.SelectedValue + "/" + years.SelectedValue).ToString();
                        }

    It is showing the 3 DDL, but when value is selected TxtBox.Text is still the first Value(it doesn`t change).

    Also here when Txtbox.Visible=false i receive an error: Input string is not in correct format  but when TxtBox.Visible=true i didn`t recieve it. That only happened on the new code.

    I have the same problem with DDL which load items from db.

    if (DataTable.Columns[i].ToString() == "a_type")
                        
                        {
                            SqlConnection con = new SqlConnection(GetConnectionString());
                            string com = "SELECT DISTINCT a_type FROM a";
                            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
                            DataSet dt = new DataSet();
                            adpt.Fill(dt);
    
                            TxtBox.Visible = false;
                            DropDownList1.DataValueField = "a_type";
                            DropDownList1.DataTextField = "a_type";
                            DropDownList1.DataSource = dt;
                            DropDownList1.DataBind();
                            DropDownList1.Attributes.Add("class", "form-control");
                            DropDownList1.ID = DataTable.Columns[i].ToString();
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(DropDownList1);
    
                            TxtBox.Text = DropDownList1.SelectedValue;
    
    
                        }

    The previous code with SQL select is working fine for me. But now when i add another code which is almost the same i still get the first value and exception with the TextBox.visible property.
    The code that doesn`t work fine:

    if (DataTable.Columns[i].ToString() == "ppp")
    
                        {
                            SqlConnection con = new SqlConnection(GetConnectionString());
                            string com = "SELECT ppp_name,ppp_id  FROM ppp";
                            SqlDataAdapter adpt = new SqlDataAdapter(com, con);
                            DataSet dt = new DataSet();
                            adpt.Fill(dt);
    
                            TxtBox.Visible = false;
                            DropDownList1.DataValueField = "ppp_id";
                            DropDownList1.DataTextField = "ppp_name";
                            DropDownList1.DataSource = dt;
                            DropDownList1.DataBind();
                            DropDownList1.Attributes.Add("class", "form-control");
                            DropDownList1.ID = DataTable.Columns[i].ToString();
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(DropDownList1);
                            
                            TxtBox.Text = DropDownList1.SelectedValue;
                        }

    Thank you for your time.

    Monday, March 15, 2021 1:39 PM

All replies

  • User475983607 posted

    Learn how to use the Visual Studio debugger.  I assume the error is coming from this line or a similar line of code.  Using the debugger you can easily see the selected values.

    TxtBox.Text = DateTime.Parse(days.SelectedValue + "/" + months.SelectedValue + "/" + years.SelectedValue).ToString();

    Keep in mind, culture setting can affect a data format.  What I find very confusing with this design is you already know the select values.  What's the purpose of placing these values in another input?  Makes no sense.

    If you must have duplicate input fields then I would just create a new DateTime using the constructor rather than all the string logic.

    new DateTime(year, month, day)

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.-ctor?view=net-5.0#System_DateTime__ctor_System_Int32_System_Int32_System_Int32_

    Monday, March 15, 2021 1:54 PM
  • User-106287432 posted

    I still recieve the same error and exception on DDL with SQL. So i think that is not the problem. But thanks :)

    Monday, March 15, 2021 2:10 PM
  • User475983607 posted

    HoneyMG

    I still recieve the same error and exception on DDL with SQL. So i think that is not the problem. But thanks :)

    You have the debugger and the line of code that caused the exception.  I do not have this information so I can only guess where the bugs are in you code.  You should, at the very least, create a new DateTime using the constructor syntax I shared above.  Always, use strong types rather than strings and relying on SQL to cast a string to a date.  Server and user culture setting affect how dates are represented on the screen and interpreted.  

    Monday, March 15, 2021 2:21 PM