ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.[Solved] Duplicate item added to DropDownList on setting SelectedItem in ASP.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p>You need to write like below code for set selected text in dropdownlist from gridview.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; ID=&#34;gvCustomers&#34; OnSelectedIndexChanged=&#34;gvCustomers_SelectedIndexChanged&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;asp:CommandField ShowSelectButton=&#34;true&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlName&#34;&gt; &lt;/asp:DropDownList&gt; &lt;br /&gt; &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlCountry&#34;&gt; &lt;/asp:DropDownList&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient Imports System.Configuration</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = GetData(); gvCustomers.DataSource = dt; gvCustomers.DataBind(); ddlCountry.DataSource = dt; ddlCountry.DataValueField = &#34;Country&#34;; ddlCountry.DataTextField = &#34;Country&#34;; ddlCountry.DataBind(); ddlCountry.Items.Insert(0, new ListItem(&#34;Select&#34;, &#34;&#34;)); ddlName.DataSource = dt; ddlName.DataValueField = &#34;Name&#34;; ddlName.DataTextField = &#34;Name&#34;; ddlName.DataBind(); ddlName.Items.Insert(0, new ListItem(&#34;Select&#34;, &#34;&#34;)); } } private static DataTable GetData() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT CustomerId, Name, Country FROM CustomerTest&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); return dt; } } } } protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e) { int index = gvCustomers.SelectedIndex; string name = gvCustomers.SelectedRow.Cells[1].Text; string country = gvCustomers.SelectedRow.Cells[2].Text; if (ddlName.Items.FindByText(name) != null) { ddlName.ClearSelection(); ddlName.Items.FindByText(name).Selected = true; if (ddlCountry.Items.FindByText(country) != null) { ddlCountry.ClearSelection(); ddlCountry.Items.FindByText(country).Selected = true; } else { ddlCountry.SelectedIndex = 0; } } if (ddlCountry.Items.FindByText(country) != null) { ddlCountry.ClearSelection(); ddlCountry.Items.FindByText(country).Selected = true; if (ddlName.Items.FindByText(name) != null) { ddlName.ClearSelection(); ddlName.Items.FindByText(name).Selected = true; } else { ddlName.SelectedIndex = 0; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = GetData() gvCustomers.DataSource = dt gvCustomers.DataBind() ddlCountry.DataSource = dt ddlCountry.DataValueField = &#34;Country&#34; ddlCountry.DataTextField = &#34;Country&#34; ddlCountry.DataBind() ddlCountry.Items.Insert(0, New ListItem(&#34;Select&#34;, &#34;&#34;)) ddlName.DataSource = dt ddlName.DataValueField = &#34;Name&#34; ddlName.DataTextField = &#34;Name&#34; ddlName.DataBind() ddlName.Items.Insert(0, New ListItem(&#34;Select&#34;, &#34;&#34;)) End If End Sub Private Shared Function GetData() As DataTable Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT CustomerId, Name, Country FROM CustomerTest&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) Return dt End Using End Using End Using End Function Protected Sub gvCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Dim index As Integer = gvCustomers.SelectedIndex Dim name As String = gvCustomers.SelectedRow.Cells(1).Text Dim country As String = gvCustomers.SelectedRow.Cells(2).Text If ddlName.Items.FindByText(name) IsNot Nothing Then ddlName.ClearSelection() ddlName.Items.FindByText(name).Selected = True If ddlCountry.Items.FindByText(country) IsNot Nothing Then ddlCountry.ClearSelection() ddlCountry.Items.FindByText(country).Selected = True Else ddlCountry.SelectedIndex = 0 End If End If If ddlCountry.Items.FindByText(country) IsNot Nothing Then ddlCountry.ClearSelection() ddlCountry.Items.FindByText(country).Selected = True If ddlName.Items.FindByText(name) IsNot Nothing Then ddlName.ClearSelection() ddlName.Items.FindByText(name).Selected = True Else ddlName.SelectedIndex = 0 End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/6EB57gE.gif" alt="" width="293" height="395" /></p>https://www.aspforums.net:443/Threads/255438/Solved-Duplicate-item-added-to-DropDownList-on-setting-SelectedItem-in-ASPNet/https://www.aspforums.net:443/Threads/255438/Solved-Duplicate-item-added-to-DropDownList-on-setting-SelectedItem-in-ASPNet/Fri, 26 Jul 2019 03:05:47 GMT