Search code examples
c#telerikselectedvalueradcombobox

how can I store the RadComboBox selected value in C# code?


My RadComboBoxis supposed to drop down and display options for me to select. the options are name but the selected values are numbers corresponding to the ID of the center.

<telerik:RadComboBox DataValueField="OPERATION_CENTER_ID" RenderingMode="Full" EmptyMessage="Select Operation Center" DataTextField="OPERATION_CENTER_NAME" ID="RadComboBox1" runat="server"></telerik:RadComboBox>

 protected void rdOrders_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e) {
     //Get OPERATION_CENTER_ID in f1 from f2 query (it is an int)

     string f2 = "select * from [dbo].[GetOperationCentersInfo]('F')";

     SQLHelper a = new SQLHelper(SQLHelper.ConnectionStrings.EmallShippingConnectionString);

       int ID = RadComboBox1.SelectedValue; {
       string f1 = "select * from [dbo].[GetReadyDeliveryOrderItems](" + ID + ")";
       DataTable DataTable1 = a.getQueryResult(f1); //Orders Query by OPERATION_CENTER_ID
       DataTable DataTable2 = a.getQueryResult(f2); //Operation centers Query
       DataTable testDataTable = new DataTable();
       rdOrders.DataSource = DataTable1;
       RadComboBox1.DataSource = DataTable2;

       RadComboBox1.DataBind();
     }

Basically when the user selects a value from RadComboBox1, I want to store that value in an int variable.


Solution

  • Hope that DataValueField is assigned while binding the grid, and the Value is convertible to an integer. Then I would like to suggest int.TryParse for this conversion, So that you use its return value to check whether the conversion is successful or not. See the code below:

    int ID;
    if(Int32.TryParse(RadComboBox1.SelectedValue,out ID))
    {
      // You can proceed with ID 
    }
    else
    {
      // Conversion failed
    }