Problem populating comboBoxes from SQL database after RequiredField Validation

2 posts, 0 answers
  1. newguy
    newguy avatar
    26 posts
    Member since:
    May 2013

    Posted 16 May 2013 Link to this post

    Hi guys,

    I have 2 ComboBoxes, The selection from the first comboBox Will populate the Second ComboBox and will bind the data from a SQL database. This all work perfectly but since i added a RequiredFieldValidator on the first ComboBox if the user fails to select a option from the first combobox the Validation Error will Fire but then when the user selects an item from the first ComboBox ofter being prompted to please make a selection the Second ComboBox fails to populate.

    Here is and example of Code i am using for the validation

    <td>
    <telerik:RadComboBox ID="cboAccount" runat="server" DataTextField="AccountName"
    DataValueField="AccountID" EnableLoadOnDemand="True" EmptyMessage="Select Account"
    MarkFirstMatch="True"
    OnSelectedIndexChanged="cboAccount_SelectedIndexChanged"
    AutoPostBack="True" >
    </telerik:RadComboBox>
     
     <asp:RequiredFieldValidator ID="ValidateAccount" runat="server"ControlToValidate="cboAccount" ErrorMessage="Please select a value"></asp:RequiredFieldValidator>
     </td>
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 May 2013 Link to this post

    Hi newguy,

    Please have a look at the sample code I tried for a similar scenario which works fine at my end.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" EnableLoadOnDemand="true" OnItemsRequested="RadComboBox1_ItemsRequested"
        EmptyMessage="Select Country" MarkFirstMatch="True" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged"
        AutoPostBack="True">
    </telerik:RadComboBox>
    <asp:RequiredFieldValidator ID="ValidateAccount" runat="server" ControlToValidate="RadComboBox1"
        ErrorMessage="Select a Country"></asp:RequiredFieldValidator>
    <br />
    <br />
    <telerik:RadComboBox ID="RadComboBox2" runat="server" Width="186px" />
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT Name FROM [Country] ORDER By Name"></asp:SqlDataSource>
    <br />
    <br />
    <telerik:RadButton ID="RadButton1" runat="server" Text="Click" CausesValidation="true">
    </telerik:RadButton>

    C#:
    protected void RadComboBox1_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        RadComboBox1.DataSourceID = "SqlDataSource2";
        RadComboBox1.DataTextField = "Name";
        RadComboBox1.DataBind();
    }
    protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        LoadCities(e.Text);
    }
    protected void LoadCities(string countryName)
    {
        SqlConnection connection = new SqlConnection(
        ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM City WHERE Countryname  in ('" + countryName + "') ORDER By Countryname,Cityname", connection);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        RadComboBox2.DataTextField = "Cityname";
        RadComboBox2.DataSource = dt;
        RadComboBox2.DataBind();
    }

    Thanks,
    Shinu.
Back to Top