3

In my webpage, there is a RadComboBox outside the RadGrid and a Dropdown inside RadGrid.

Data inside Dropdown is bind based on RadComboBox item selection.
Ex: If from RadComboBox, item "Company" is selected, then data inside Dropdown will be related to "Company" (i.e, Company1, Company2,company3, etc)

HTML code:

<telerik:RadComboBox ID="ddlCompany" runat="server" Height="200" Width="240"
          DropDownWidth="310" EmptyMessage="- Select Product -" HighlightTemplatedItems="true" CausesValidation="false" Filter="StartsWith" AppendDataBoundItems="true" AllowCustomText="true" AutoPostBack="true" DataTextField="Title" DataValueField="Code" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
</telerik:RadComboBox>

<telerik:RadGrid ID="RGGSTAcCode" runat="server">       
    <Columns> 
         <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn> 

         <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
             <ItemTemplate>
                 <asp:Label ID="lblAcCode" Text='<%# Eval("AccountCode") %>' runat="server"></asp:Label>
             </ItemTemplate>
             <EditItemTemplate>
                 <asp:DropDownList ID="ddlAcCode" DataTextField="AccountDescription" DataValueField="AccountCodeID" runat="server"/> 
             </EditItemTemplate>
         </telerik:GridTemplateColumn>
     </Columns>

C# Code:

public DataSet GetCompanyNames()
{
    SqlConnection con = new SqlConnection(strcon);
    SqlCommand cmd = new SqlCommand("General.usp_tbl_BuyerCode_Query", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    try
    {
        con.Open();
        da.Fill(ds);
        con.Close();
    }
    catch (Exception ex)
    {
    }
    return ds;
}

protected void BindComapnyDL()
{
    ddlCompany.DataTextField = "Title";
    ddlCompany.DataValueField = "Code";
    ddlCompany.DataSource = GetCompanyNames();
    ddlCompany.DataBind();

    Session["Comp"] = ddlCompany.SelectedValue.ToString();
}

protected void ddlCompany_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    if (ddlCompany.SelectedItem != null)
    {
        SqlConnection con = new SqlConnection(strcon);
        SqlDataAdapter adapter = new SqlDataAdapter();

        adapter.SelectCommand = new SqlCommand("SELECT [AccountCodeID],[AccountCode]+' - '+[AccountDescription] as[AccountDescription] FROM [Sunway_AP].[General].[tbl_AccountCode] (NOLOCK) Where [CompanyCode] = '" + Session["Comp"] + "' order by [AccountCode]+' - '+[AccountDescription]", con);
        con.Open();
        DataTable dt = new DataTable();
        try
        {
            adapter.Fill(dt);
        }
        finally
        {
            con.Close();
        }

        DropDownList list = RGGSTAcCode.FindControl("ddlAcCode") as DropDownList;
        list.DataTextField = "AccountDescription";
        list.DataValueField = "AccountCodeID";
        list.DataSource = dt;
        list.DataBind();
    }
    else
    {
        Response.Write("Please select Company first");
    }
}

Now, when I try to change the company using "ddlCompany_SelectedIndexChanged" event,

I get below error:
System.NullReferenceException: Object reference not set to an instance of an object.
at line :
list.DataTextField = "AccountDescription";

Please suggest what is wrong in my code. Thanks in advance

Stephan
  • 4,187
  • 1
  • 21
  • 33
timz_123
  • 435
  • 1
  • 9
  • 47
  • If you put a breakpoint at this line DropDownList list = RGGSTAcCode.FindControl("ddlAcCode") as DropDownList; you will see that list is null. That's why the error. – Thanos Markou Jul 07 '15 at 09:53
  • @Thanos Markou: Thank you for the reply. Yes you are right, I checked it using breakpoint and found that the list is null. I am using this asp Dropdown inside RadGrid "EditItemTemplate" and trying to find this control inside RadComboBox (which is outside of RadGrid) "SelectedIndexChanged" event. Please let me know what shall I correct in my code so that it work as expected? – timz_123 Jul 08 '15 at 02:56

2 Answers2

0

This line contains error dropdown is not properly find

DropDownList list = RGGSTAcCode.FindControl("ddlAcCode") as DropDownList;

Where did You placed Dropdown inside any control..??

visite this link http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/operations-with-ms-dropdownlist-in-edititemtemplate-of-gridtemplatecolumn

  • Thank you for the reply. I am using this asp Dropdown inside Telerik RadGrid "EditItemTemplate" and trying to find this control inside RadComboBox (which is outside of RadGrid) "SelectedIndexChanged" event. Please let me know what shall I correct in my code so that it work as expected? Also, I checked the suggested link but it couldn't help me in my issue. Please reply – timz_123 Jul 08 '15 at 02:59
0

try this

 foreach (GridDataItem item in RGGSTAcCode.EditItems)
{
    DropDownList list = item.FindControl("ddlAcCode") as DropDownList;
    //Now we can do stuff with the "list"
}

Keep in mind that the row needs to be in Edit Mode to find controls in its EditItemTemplate.

psoshmo
  • 1,490
  • 10
  • 19
  • 1
    Thankyou for the reply. Unfortunately your solution is not working as per my requirement. I have to bind the asp Dropdown (which is inside Telerik RadGrid "EditItemTemplate" ) on RadComboBox "SelectedIndexChanged" event (which is outside of Telerik RadGrid). Because the asp Dropdown data is based on RadComboBox item selection. Please suggest – timz_123 Jul 08 '15 at 03:03
  • @user3196511 if telerik works like I assume it does, the control in the edititemtemplate doesn't actually exist until the row is in edit mode. If that is the case, you would bind it in the init event of the control inside the template – psoshmo Jul 08 '15 at 03:06