Ads 468x60px

Labels

.

Wednesday, 22 May 2013

Insert multiple selected items of ListBox into SQL Server database

Insert multiple selected items of ListBox into SQL Server database

Introduction : -
In this tutorial, we will learn how to create a ListBox control at
design-time as well as at run-time. We will also see how to create a
multiple-column ListBox control with single and multiple selections.
This article also covers most of the properties and methods of the
ListBox control.

Description :- In my previous article i have used to save list box selected data in multiple column.
This example shows the basics on how to save multiple selected items from the ASP.Net ListBox control to the database in ASP.Net. Please note that this example requires a basic knowledge of ADO.NET.

STEP1: Setting up the User Interface (GUI)

For the simplicity of this demo, I just set up the web form like below:

* Since the ListBox is intended for multiple item selections then we
need to set the SelectionMode attribute of the ListBox to Multiple

* To do multiple Selections in the ListBox then just hold Ctrl key and select the items you want.
STEP 2: Creating a Simple Database Table

In this demo, we are going to store the selected employee names that is
selected from the ListBox to the database. So let's now create a simple
table that contains the following Column Names:Note [-id is auto increment]

Note:
I set the Id to auto increment so that the id will be automatically
generated for every new added row. To do this select the Column name
“Id” and in the column properties set the “Identity Specification” to
yes.

STEP 3: Declaring the necessary name spaces:

Be sure to add the following namespaces below:

using System.Data.SqlClient;

using System.Collections.Specialized;

using System.Text;

We need to declare the namespaces above so that we can use the
SqlClient, StrngCollections and StringBuilder built-in methods in our
codes later.

STEP4: Creating the Method for Multiple Inserts.

Here are the code blocks below:

privatestring GetConnectionString()

{

//Where DBConnection is the connetion string that was set up in the web config file