Insert all the selected items from the listbox to a database when we click insert button

In this blog we will know how to insert all the selected items from the listbox to a database when we click insert button. Before clicking insert button we have to choose all the items from the listbox.

 

 

Scenario: - There are two list boxes and five buttons. In the first listbox data will be bind at runtime when page loads. The four buttons are used to transfer items from one listbox1 to listbox2 and vice-versa. And at last there is insert button.

 

Note: - For listbox2 we have to assign    SelectionMode="Multiple"

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Listboxes._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:ListBox ID="ListBox1" runat="server" Width="134px" Height="116px"></asp:ListBox>

    <asp:Button ID="Button1" runat="server" Text="&lt;|" onclick="Button1_Click" />

   <asp:Button ID="Button2" runat="server" Text="|&gt;" onclick="Button2_Click" />

   <asp:Button ID="Button3" runat="server" Text="&lt;&lt;|" onclick="Button3_Click" />

   <asp:Button ID="Button4" runat="server" Text="|&gt;&gt;" onclick="Button4_Click" />

    <asp:ListBox ID="ListBox2" runat="server" Width="134px" Height="116px" SelectionMode="Multiple"></asp:ListBox>

    </div>

    <asp:Button ID="btn_insert" runat="server" Text="Insert"

        onclick="btn_insert_Click" />

    </form>

   

</body>

</html>

 

 

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

namespace Listboxes

{

    public partial class _Default : System.Web.UI.Page

    {

        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        string str;

        SqlCommand com;

 

        protected void Page_Load(object sender, EventArgs e)

        {

           

            SqlConnection con = new SqlConnection(strConnString);

 

            if (!IsPostBack)

            {

                con.Open();

                str = "select * from  employee";

                com = new SqlCommand(str, con);

                SqlDataReader reader = com.ExecuteReader();

                while (reader.Read())

                {

                    ListBox1.Items.Add(reader["empname"].ToString());

                }

                reader.Close();

                con.Close();

            }

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            ListBox1.Items.Add(ListBox2.SelectedItem.Text);

            int i = 0;

            i = ListBox2.SelectedIndex;

            ListBox2.Items.RemoveAt(i);

        }

 

        protected void Button2_Click(object sender, EventArgs e)

        {

            ListBox2.Items.Add(ListBox1.SelectedItem.Text);

            int i = 0;

            i = ListBox1.SelectedIndex;

            ListBox1.Items.RemoveAt(i);

        }

 

        protected void Button3_Click(object sender, EventArgs e)

        {

            int j = 0;

            for (j = 0; j <= ListBox2.Items.Count - 1; j++)

            {

                ListBox1.Items.Add(ListBox2.Items[j]);

            }

            ListBox2.Items.Clear();

        }

 

        protected void Button4_Click(object sender, EventArgs e)

        {

            int j = 0;

            for (j = 0; j <= ListBox1.Items.Count - 1; j++)

            {

                ListBox2.Items.Add(ListBox1.Items[j]);

            }

            ListBox1.Items.Clear();

        }

 

        protected void btn_insert_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(strConnString);

            con.Open();

            for (int i = 0; i < ListBox2.Items.Count; i++)

            {

                if (ListBox2.Items[i].Selected == true)

                {

                    str = "insert into employee1 values('" + ListBox2.Items[i].ToString() + "')";

                    com = new SqlCommand(str, con);

                    com.ExecuteNonQuery();

 

                }

            }

            Response.Write("Inserted");

        }

    }

}

 

 

 

Thanks for reading