Error on the "SelectedIndexChanged" dropdown event, as well as removing a new item from RadComboBox

My webpage has a RadComboBox outside RadGrid and Dropdown inside RadGrid.

The data inside the dropdown list is associated with the selection of RadComboBox items.
Example . If "Company" is selected from RadComboBox, the data in the drop-down list will be associated with "Company" (for example, 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 the "ddlCompany_SelectedIndexChanged" event,

I am getting below error:
System.NullReferenceException: Object reference not set to object instance.
in the line:
list.DataTextField = "AccountDescription";

Please point out what is wrong in my code. thanks in advance

+3


source to share


2 answers


This line contains the error not found dropdown

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



Where have you placed Dropdown inside any control.

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

0


source


try it

 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 a row must be in edit mode in order to find controls in its EditItemTemplate.

0


source







All Articles