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.Insert (Save) Multiselect DropDownList value in Database in ASP.Net using C# and VB.Net<p>HiBmChrist,</p> <p>Refer the below modified code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#34;.js-example-placeholder-single&#34;).select2({ placeholder: &#34;Select&#34;, allowClear: false }); $(&#39;#ddl1&#39;).on(&#39;change&#39;, function () { $(&#39;#&lt;%=hfSelected.ClientID%&gt;&#39;).val($(this).val()); }); }); &lt;/script&gt; &lt;asp:DropDownList ID=&#34;ddl1&#34; Width=&#34;300px&#34; runat=&#34;server&#34; multiple=&#34;multiple&#34; CssClass=&#34;form-control js-example-placeholder-single&#34; ToolTip=&#34;Select &#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:HiddenField ID=&#34;hfSelected&#34; runat=&#34;server&#34; /&gt; &lt;asp:TextBox ID=&#34;Keyword_enter&#34; runat=&#34;server&#34; Width=&#34;100%&#34;&gt;&lt;/asp:TextBox&gt; &lt;asp:Button ID=&#34;btnSave&#34; Text=&#34;Save &#34; runat=&#34;server&#34; OnClick=&#34;btnSave_Click&#34; /&gt; &lt;asp:Literal ID=&#34;Lit2&#34; runat=&#34;server&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">C#</span></strong></p> <pre class="brush: csharp">protected void btnSave_Click(object sender, EventArgs e) { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection Conn3 = new SqlConnection(conString); SqlCommand sqlCom = new SqlCommand(&#34;INSERT INTO Keyword VALUES(@Keyword)&#34;, Conn3); Conn3.Open(); string keyWords = Keyword_enter.Text.Trim() + &#34;,&#34; + hfSelected.Value; string[] arrayval = keyWords.Trim().Split(&#39;,&#39;); for (int i = 0; i &lt; arrayval.Length; i++) { if (!string.IsNullOrEmpty(arrayval[i])) { sqlCom.Parameters.Clear(); sqlCom.Parameters.AddWithValue(&#34;@Keyword&#34;, arrayval[i]); sqlCom.ExecuteNonQuery(); Lit2.Text = &#34;recorded &#34;; } Keyword_enter.Text = String.Empty; } ddl1.SelectedIndex = -1; hfSelected.Value = &#34;&#34;; Conn3.Close(); }</pre> <p><span style="text-decoration: underline;"><strong>VB.Net</strong></span></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then PopulateDropDownList() End If End Sub Private Sub PopulateDropDownList() Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim query As String = &#34;SELECT DISTINCT Country FROM Customers WHERE Country IS NOT NULL&#34; Dim cmd As SqlCommand = New SqlCommand(query) Using con As SqlConnection = New SqlConnection(conString) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using ds As DataSet = New DataSet() sda.Fill(ds) ddl1.DataSource = ds ddl1.DataTextField = &#34;Country&#34; ddl1.DataValueField = &#34;Country&#34; ddl1.DataBind() End Using End Using End Using End Sub Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim Conn3 As SqlConnection = New SqlConnection(conString) Dim sqlCom As SqlCommand = New SqlCommand(&#34;INSERT INTO Keyword VALUES(@Keyword)&#34;, Conn3) Conn3.Open() Dim keyWords As String = Keyword_enter.Text.Trim() &amp; &#34;,&#34; + hfSelected.Value Dim arrayval As String() = keyWords.Trim().Split(&#34;,&#34;c) For i As Integer = 0 To arrayval.Length - 1 If Not String.IsNullOrEmpty(arrayval(i)) Then sqlCom.Parameters.Clear() sqlCom.Parameters.AddWithValue(&#34;@Keyword&#34;, arrayval(i)) sqlCom.ExecuteNonQuery() Lit2.Text = &#34;recorded &#34; End If Keyword_enter.Text = String.Empty Next ddl1.SelectedIndex = -1 hfSelected.Value = &#34;&#34; Conn3.Close() End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong>The Form</strong></p> <p><strong><img src="https://i.imgur.com/2LXT6Xd.gif" alt="" width="346" height="303" /></strong></p> <p><strong>Record After Insert in DataBase</strong></p> <p><strong><img src="https://i.imgur.com/8zNWimd.jpg" alt="" width="196" height="192" /></strong></p>https://www.aspforums.net:443/Threads/130407/Insert-Save-Multiselect-DropDownList-value-in-Database-in-ASPNet-using-C-and-VBNet/https://www.aspforums.net:443/Threads/130407/Insert-Save-Multiselect-DropDownList-value-in-Database-in-ASPNet-using-C-and-VBNet/Wed, 19 Dec 2018 23:41:52 GMT