This is a migrated thread and some comments may be shown as answers.

Problem for binding radcombox in detailview

1 Answer 95 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
rabbiwan
Top achievements
Rank 1
rabbiwan asked on 30 Jul 2008, 10:04 AM
Hello,
I would like binding two radcombobox in a DetailView, for manage postcode(RadCboCodePostal) and twon(RadCboCommune).
But in one of the radcombobox when I bind the data from the DetailView, it's not find the item.
The error is 'RadCboCommune' has a SelectedValue which is invalid because it does not exist in the list of items
Because the data from the detailView (ADR_COM) it's 'NY' and in the radcombobox there is 'New York'
I have find some post with this problem but for the radgrid.
http://www.telerik.com/help/aspnet/grid/grderrormessages.html
I had test some solution in the detailView but it's don't work.
Do you know a solution for this problem?
Thank you
Regards

    <form id="form1" runat="server"
 
      <asp:ScriptManager ID="ScriptManager1" runat="server"
        </asp:ScriptManager> 
        <asp:DetailsView BorderWidth="0px" ID="DetailsView1" CssClass="DetailsView" runat="server" 
            GridLines="None" AutoGenerateRows="False" DataKeyNames="SOC_ID" DataSourceID="SqlDataSourceSociete" 
            Height="300px" Width="300px"  OnItemCommand="DetailsView1_ItemCommand" > 
            <Fields> 
                
                <asp:TemplateField HeaderText="Code postal"
                    <EditItemTemplate> 
                        <telerik:RadComboBox ID="RadCboCodePostal" DataValueField="RCO_CP" DataSourceID="SqlDataSourceCodePostal" 
                            Skin="Web20" HighlightTemplatedItems="true" AutoPostBack="true" OnSelectedIndexChanged="RadCboCodePostal_SelectedIndexChanged" 
                            DataTextField="RCO_CP" SelectedValue='<%#Bind("ADR_CP") %>' runat="server" AllowCustomText="True" 
                            Filter="Contains" ShowMoreResultsBox="True" EnableLoadOnDemand="True" MarkFirstMatch="True" 
                            OnItemsRequested="RadCboCodePostal_ItemsRequested" EnableVirtualScrolling="True"
                        </telerik:RadComboBox> 
                    </EditItemTemplate> 
                    <HeaderStyle CssClass="EditDetailsFormLabel" /> 
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Commune"
                    <EditItemTemplate> 
                        <asp:Label ID="lblCommune" runat="server" Text=""></asp:Label><br /> 
                        <telerik:RadComboBox ID="RadCboCommune" AppendDataBoundItems="True" DataValueField="RCO_COM" 
                            DataSourceID="SqlDataSourceCommune" Skin="Web20" HighlightTemplatedItems="true" 
                            Filter="Contains" DataTextField="RCO_COM" runat="server"  SelectedValue='<%# Bind("ADR_COM") %>' > 
                            <Items> 
                                <telerik:RadComboBoxItem runat="server" Selected="True" /> 
                            </Items> 
                        </telerik:RadComboBox> 
                    </EditItemTemplate> 
                    <HeaderStyle CssClass="EditDetailsFormLabel" /> 
                </asp:TemplateField>             
                <asp:CommandField ButtonType="Button" ShowEditButton="True"
                    <ControlStyle CssClass="EditFormActionButtons" /> 
                </asp:CommandField> 
                <asp:CommandField ButtonType="Button" ShowInsertButton="True"
                    <ControlStyle CssClass="EditFormActionButtons" /> 
                </asp:CommandField> 
            </Fields> 
        </asp:DetailsView> 
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
        </telerik:RadAjaxManager> 
        <asp:SqlDataSource ID="SqlDataSourceCodePostal" runat="server" ConnectionString="<%$ ConnectionStrings:DB_DORY_ConnectionString %>" 
            SelectCommand="SELECT DISTINCT TR_COMMUNE_RCO.RCO_CP FROM T_ADRESSE_ADR INNER JOIN T_SOCIETE_SOC ON T_ADRESSE_ADR.ADR_ID = T_SOCIETE_SOC.SOC_ADR INNER JOIN TR_COMMUNE_RCO ON T_ADRESSE_ADR.ADR_CP = TR_COMMUNE_RCO.RCO_CP WHERE (T_SOCIETE_SOC.SOC_ID = @Society) ORDER BY TR_COMMUNE_RCO.RCO_CP"
            <SelectParameters> 
                <asp:QueryStringParameter DefaultValue="0" Name="Society" QueryStringField="society" 
                    Type="String" /> 
            </SelectParameters> 
        </asp:SqlDataSource> 
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 
        <asp:SqlDataSource ID="SqlDataSourceCommune" runat="server" ConnectionString="<%$ ConnectionStrings:DB_DORY_ConnectionString %>" 
            SelectCommand="SELECT DISTINCT TR_COMMUNE_RCO.RCO_COM FROM T_ADRESSE_ADR INNER JOIN T_SOCIETE_SOC ON T_ADRESSE_ADR.ADR_ID = T_SOCIETE_SOC.SOC_ADR INNER JOIN TR_COMMUNE_RCO ON T_ADRESSE_ADR.ADR_CP = TR_COMMUNE_RCO.RCO_CP WHERE (T_SOCIETE_SOC.SOC_ID = @Society) ORDER BY TR_COMMUNE_RCO.RCO_COM"
            <SelectParameters> 
                <asp:QueryStringParameter DefaultValue="0" Name="Society" QueryStringField="society" 
                    Type="String" /> 
            </SelectParameters> 
        </asp:SqlDataSource> 
   
        <asp:SqlDataSource ID="SqlDataSourceSociete" runat="server" ConnectionString="<%$ ConnectionStrings:DB_DORY_ConnectionString %>" 
            InsertCommand="XTU_AddBaseSocieteSociete" InsertCommandType="StoredProcedure" 
            SelectCommand="SELECT T_SOCIETE_SOC.SOC_ID, T_ADRESSE_ADR.ADR_CP, T_ADRESSE_ADR.ADR_COM FROM T_SOCIETE_SOC INNER JOIN T_ADRESSE_ADR ON T_SOCIETE_SOC.SOC_ADR = T_ADRESSE_ADR.ADR_ID WHERE (T_SOCIETE_SOC.SOC_ID = @Society)" 
            UpdateCommand="XTU_UpBaseSocieteSociete" UpdateCommandType="StoredProcedure"
            <UpdateParameters> 
                <asp:Parameter Name="SOC_ID" Type="Int32" /> 
                <asp:Parameter Name="ADR_CP" Type="String" /> 
                <asp:Parameter Name="ADR_COM" Type="String" /> 
            </UpdateParameters> 
            <InsertParameters> 
                <asp:Parameter Direction="InputOutput" Name="SOC_ID" Type="Int32" /> 
                <asp:Parameter Name="ADR_CP" Type="String" /> 
                <asp:Parameter Name="ADR_COM" Type="String" /> 
            </InsertParameters> 
            <SelectParameters> 
                <asp:QueryStringParameter DefaultValue="0" Name="Society" QueryStringField="Society" 
                    Type="String" /> 
            </SelectParameters> 
        </asp:SqlDataSource> 
       
    </form> 
 

public partial class WebForm1 : System.Web.UI.Page 
    { 
        public static String ConnString = ConfigurationManager.ConnectionStrings["DB_DORY_ConnectionString"].ConnectionString; 
 
        protected void Page_Load(object sender, EventArgs e) 
        { 
               base.OnInit(e); 
               if (!this.IsPostBack) 
               { 
                   DetailsView1.DefaultMode = DetailsViewMode.Edit; 
               } 
        } 
 
        public static DataTable GetRelatedRecords(string query) 
        { 
            SqlConnection conn = new SqlConnection(ConnString); 
            SqlDataAdapter adapter = new SqlDataAdapter(); 
            adapter.SelectCommand = new SqlCommand(query, conn); 
 
            DataTable myDataTable = new DataTable(); 
 
            conn.Open(); 
 
            try 
            { 
                adapter.Fill(myDataTable); 
            } 
            finally 
            { 
                conn.Close(); 
            } 
            return myDataTable; 
        } 
 
        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) 
        { 
             
 
        } 
 
        protected void RadCboCodePostal_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e) 
        { 
            DataTable data = GetRelatedRecords("SELECT DISTINCT [RCO_CP] FROM [dbo].[TR_COMMUNE_RCO] WHERE [RCO_CP] LIKE '" + e.Text + "%'"); 
 
            RadComboBox List = (RadComboBox)sender; 
            List.AutoPostBack = true
            try 
            { 
                int itemsPerRequest = 10; 
                int itemOffset = e.NumberOfItems; 
                int endOffset = itemOffset + itemsPerRequest; 
                if (endOffset > data.Rows.Count) 
                { 
                    endOffset = data.Rows.Count; 
                } 
                if (endOffset == data.Rows.Count) 
                { 
                    e.EndOfItems = true
                } 
                else 
                { 
                    e.EndOfItems = false
                } 
                for (int i = itemOffset; i < endOffset; i++) 
                { 
                    List.Items.Add(new RadComboBoxItem(data.Rows[i]["RCO_CP"].ToString(), data.Rows[i]["RCO_CP"].ToString())); 
                } 
 
                if (data.Rows.Count > 0) 
                { 
                    e.Message = String.Format("ElĂ©ment <b>1</b>-<b>{0}</b> sur <b>{1}</b>", endOffset.ToString(), data.Rows.Count.ToString()); 
                } 
                else 
                { 
                    e.Message = "Pas de correspondance"
                } 
            } 
            catch 
            { 
                e.Message = "Pas de correspondance"
            } 
        } 
 
        protected void RadCboCodePostal_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e) 
        { 
            DetailsView editedItem = (sender as RadComboBox).NamingContainer as DetailsView; 
            if (((RadComboBox)editedItem.FindControl("RadCboCodePostal")).SelectedValue != ""
            { 
 
                RadComboBox ddList = (RadComboBox)editedItem.FindControl("RadCboCommune"); 
 
                DataTable table = GetRelatedRecords("SELECT DISTINCT [RCO_COM] FROM [dbo].[TR_COMMUNE_RCO] WHERE RCO_CP = '" + ((RadComboBox)editedItem.FindControl("RadCboCodePostal")).SelectedValue + "' ORDER BY [RCO_COM]"); 
 
                ddList.DataSourceID = ""
                ddList.DataSource = table; 
                ddList.DataValueField = "RCO_COM"
                ddList.DataTextField = "RCO_COM"
                ddList.DataBind(); 
            } 
        } 
    } 




1 Answer, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 01 Aug 2008, 12:58 PM
Hello rabbiwan,

This is the normal behavior.

The values of the ADR_COM in detailsview's datasource should exactly match the values from the SqlDataSourceCommune datasource.

So, you should ensure that both datasources contains New York (or NY).

Kind regards,
Veskoni
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
ComboBox
Asked by
rabbiwan
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Share this question
or