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.Save multiple selected items of ASP.Net ListBox using C# and VB.Net<p>Hi&nbsp;sanjayjp13,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>Database</p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:ListBox ID=&#34;lbCountries&#34; runat=&#34;server&#34; Width=&#34;200px&#34; SelectionMode=&#34;Multiple&#34;&gt; &lt;/asp:ListBox&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Get Selected Item&#34; runat=&#34;server&#34; OnClick=&#34;GetSelectedItem&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Text;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Text</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PopulateCountries(); } } private void PopulateCountries() { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT TOP 10 CustomerID,Country FROM Customers&#34;; cmd.Connection = conn; conn.Open(); DataTable dt = new DataTable(); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); lbCountries.DataSource = dt; lbCountries.DataTextField = &#34;Country&#34;; lbCountries.DataValueField = &#34;CustomerID&#34;; lbCountries.DataBind(); } conn.Close(); } } } protected void GetSelectedItem(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); foreach (ListItem item in lbCountries.Items) { if (item.Selected) { sb.Append(item.Text + &#34;,&#34;); } } Response.Write(sb.ToString().Remove(sb.ToString().Length - 1)); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then PopulateCountries() End If End Sub Private Sub PopulateCountries() Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand() cmd.CommandText = &#34;SELECT TOP 10 CustomerID,Country FROM Customers&#34; cmd.Connection = conn conn.Open() Dim dt As DataTable = New DataTable() Using da As SqlDataAdapter = New SqlDataAdapter(cmd) da.Fill(dt) lbCountries.DataSource = dt lbCountries.DataTextField = &#34;Country&#34; lbCountries.DataValueField = &#34;CustomerID&#34; lbCountries.DataBind() End Using conn.Close() End Using End Using End Sub Protected Sub GetSelectedItem(ByVal sender As Object, ByVal e As EventArgs) Dim sb As StringBuilder = New StringBuilder() For Each item As ListItem In lbCountries.Items If item.Selected Then sb.Append(item.Text &amp; &#34;,&#34;) End If Next Response.Write(sb.ToString().Remove(sb.ToString().Length - 1)) End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/H2I4OL3.jpg" alt="" width="249" height="211" /></span></strong></p>https://www.aspforums.net:443/Threads/852081/Save-multiple-selected-items-of-ASPNet-ListBox-using-C-and-VBNet/https://www.aspforums.net:443/Threads/852081/Save-multiple-selected-items-of-ASPNet-ListBox-using-C-and-VBNet/Tue, 08 Jan 2019 00:43:10 GMT