locked
DropdownList.SelectedValue = dataReader[columnName] NOT WORKING RRS feed

  • Question

  • User2037455357 posted


    Hello there

    Really sorry I posted this quite late for me last night and didn't post correctly.  See C# code below

     string CheckIsNull(string IsStringNull)
    
                        {
    
                            if (IsStringNull == null)
                            {
                                IsStringNull = "";
                            }
    
                            return IsStringNull;
                        }
    
                        DBconnection.Open();
                        
                                SqlCommand SqlCommand_Sql_Clients = new SqlCommand(SQL_Clients, DBconnection);
    
                                SqlDataReader DataReader_Sql_Clients = SqlCommand_Sql_Clients.ExecuteReader();
    
                                DataReader_Sql_Clients.Read();
    
                        string Type_ID = DataReader_Sql_Clients["Company_Type_ID"].ToString(); // This works fine 
    
                      
                        ClientRepDet_CompanyType_DropDnLst.SelectedValue = DataReader_Sql_Clients["Company_Type_ID"].ToString();  // problem
    
                        ClientRepDet_CompanySuffix_DropDnLst.SelectedValue = Type_ID;  This works fine

    See  company_Type drop down below.  as you can see I have a connection setup already so values will already be loaded when page loads.

      <asp:DropDownList ID="ClientRepDet_CompanyType_DropDnLst" runat="server" Width="200px" DataSourceID="CompanyType_DataSource" DataTextField="Company_Type" DataValueField="ID"></asp:DropDownList>
    
                         <asp:SqlDataSource ID="CompanyType_DataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Risk_AssessmentConnectionString %>" SelectCommand="SELECT [ID], [Company_Type] FROM [Repository_Company_Type]"></asp:SqlDataSource>
    

    I have included the CheckIsNull function but in this case it doesn't really matter as I don't use it with the dropdown assignments

    Regards

    Rob

    Wednesday, September 11, 2019 9:56 PM

All replies

  • User475983607 posted

    There's not enough code to understand what is wrong.   A common cause of this symptom is a timing issue where the selected value is assigned before binding the dropdown.

    We cannot see what CheckIsNull does and the method could return an unexpected value.  On a side note, the line of code below will throw an exception if DataReader_Sql_Clients["Legal_Name"] due to the .ToString().

    ClientRepDet_status_Dropdown.selectedvalue = CheckIsNull(DataReader_Sql_Clients["Legal_Name"].ToString()); 

    I recommend setting a break point and debugging the code.

    Wednesday, September 11, 2019 11:33 PM
  • User288213138 posted

    Hi masterdineen,

    ClientRepDet_status_Dropdown.selectedvalue = CheckIsNull(DataReader_Sql_Clients[&quot;Legal_Name&quot;].ToString())

    As all-start says, setting a break pointde and bugging your code to solve your problems is a great way to do it.

    If you haven't solved your problem, please post your CheckIsNull () method.

    Best regards,

    Sam

    Thursday, September 12, 2019 2:01 AM
  • User-1716253493 posted

    You can get string Type_ID value, why not try to get string Legal_Name

    if(ClientRepDet_status_Dropdown.Items.FindByValue(Legal_Name)!=null)
    {
       ClientRepDet_status_Dropdown.Items.FindByValue(Legal_Name).Selected=true;
    }

    Thursday, September 12, 2019 2:38 AM
  • User2037455357 posted

    hi sorry,

    I have updated my post, it was quite late for me to post something and I rushed it.

    what is strange I can assign a variable with a column the dataReader successfully but not to a dropdown.selectedvalue

    string Type_ID = DataReader_Sql_Clients["Company_Type_ID"].ToString(); // This works fine 
    
                      
                        ClientRepDet_CompanyType_DropDnLst.SelectedValue = DataReader_Sql_Clients["Company_Type_ID"].ToString();  // Throws Error
    
                        ClientRepDet_CompanySuffix_DropDnLst.SelectedValue = Type_ID;  This works fine


     

    Thursday, September 12, 2019 8:25 AM
  • User-1716253493 posted

    Ensure the ddl has items populated already

    If the value is not always exist in the ddl items, use findbyvalue like my sample code to select the item without not exist error.

    Friday, September 13, 2019 7:00 AM
  • User288213138 posted

    Hi masterdineen,

    string Type_ID = DataReader_Sql_Clients["Company_Type_ID"].ToString(); // This works fine 
    
                      
    ClientRepDet_CompanyType_DropDnLst.SelectedValue = DataReader_Sql_Clients["Company_Type_ID"].ToString();  // Throws Error

    what Error did the code throws?

    You can try using break points and dubbging to see the value of the ClientRepDet_CompanyType_DropDnLst.SelectedValue

    and DataReader_Sql_Clients["Company_Type_ID"].ToString().

    Best regards,

    Sam

    Wednesday, September 18, 2019 8:12 AM