1

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.

ytba92
  • 55
  • 13
  • 1
    are you sure that SelectedValue is an int? what was its value under debug? – BugFinder Jul 19 '16 at 08:02
  • I'm trying to save the selectedValue as an integer that's my problem, it's of type string because when I tried directly saving the selectedValue in an int variable it said "cannot implicitly convert type string to int – ytba92 Jul 19 '16 at 09:06
  • that didnt answer my question at all.. what is the value in "selectedvalue" ?? debug it, look at it, show it here – BugFinder Jul 19 '16 at 09:07
  • It's empty at first page load – ytba92 Jul 19 '16 at 09:09
  • well there you go, empty is not an integer is it – BugFinder Jul 19 '16 at 09:10
  • But the problem is when I put the statement if (radComboBox1.selectedValue == null), it skips the if statement. How can I make it wait for me to select a value and then do the processing? – ytba92 Jul 19 '16 at 09:11
  • You've provided next to nothing on what you're actually doing, so it would only be speculation - but, if you're getting this when it first opens.. it should skip the if statements!! then, set the values.. it will change.. etc – BugFinder Jul 19 '16 at 09:31
  • If you need any more additional information please let me know, the page isn't even loading for me to be able to select statements – ytba92 Jul 19 '16 at 09:40
  • Ive just explained what to do.. Im not you're mother, Im not going to be doing it for you – BugFinder Jul 19 '16 at 09:45

2 Answers2

0

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
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

Is there any particular reason that you have to data-bind your RadComboBox control in a NeedDataSource event? This will not guarantee that your RadComboBox control will be loaded correctly due to the life cycle of your page/control.

What you possibly can do is to add your data-binding code to Page_Load event, something like below.

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      BindRadComboBox();
   }
}

private void BindRadComboBox()
{
    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();
}

You will need to double check the code that fetches the data though. Also for converting string to integer, please refer to un-lucky's answer, which is recommended way of doing it.

Hope this helps.

woodykiddy
  • 6,074
  • 16
  • 59
  • 100