In this article I will explain with an example, how to save multiple items from the ASP.Net ListBox control to the
SQL Server database in ASP.Net using C# and VB.Net.
Database
I have made use of the following table Fruits with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of:
ListBox – For displaying data.
The ListBox has been assigned with SelectionMode property set as Multiple for selecting multiple items.
Button – For inserting data into
SQL Server database.
The Button has been assigned with an OnClick event handler.
<b>Fruits Names:</b>
<br />
<br />
<asp:ListBox ID="lstFruits" runat="server" SelectionMode="Multiple">
<asp:ListItem>Mango</asp:ListItem>
<asp:ListItem>Orange</asp:ListItem>
<asp:ListItem>Banana</asp:ListItem>
<asp:ListItem>Apple</asp:ListItem>
<asp:ListItem>Papaya</asp:ListItem>
<asp:ListItem>Grape</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="btnInsert" runat="server" Text="Save" OnClick="Insert" />
Namespaces
You will need to import following namespaces.
C#
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
VB.Net
Imports System
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Insert Multiple Selected Items
When the Insert button is clicked, the FOR EACH loop is executed over the ListBox items.
A check is performed whether the item is selected and if selected then InsertFruit method is called with passing selected item as parameter which will be described later in the article.
Finally, the success message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
Note: For selecting multiple items just hold Ctrl key and select the items you want to add.
C#
protected void Insert(object sender, EventArgs e)
{
foreach (ListItem item in lstFruits.Items)
{
if (item.Selected)
{
this.InsertFruit(item.Text);
}
}
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Records Successfuly Saved!');", true);
}
VB.Net
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
For Each item As ListItem In lstFruits.Items
If item.Selected Then
Me.InsertFruit(item.Text)
End If
Next
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('Records Successfuly Saved!');", True)
End Sub
InsertFruit Method
This method accepts selected item from ListBox as parameter.
Then, an object of SqlCommand class is created and the INSERT query is passed to it as parameter.
The value of the selected item i.e. Fruit name is added as parameter to SqlCommand object.
And the connection is opened and the
ExecuteNonQuery function is executed and selected item will be inserted into the
SQL Server database.
C#
private void InsertFruit(string fruitName)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Fruits (FruitName) VALUES (@FruitName)";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@FruitName", fruitName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Private Sub InsertFruit(ByVal fruitName As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO Fruits (FruitName) VALUES (@FruitName)"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@FruitName", fruitName)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Screenshot
Downloads