Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » ASP.NET & Web Development » How to save multiple selected list items in database and dropdowns list items?
       
Author Reply
fahad sheikhji
posted 22 posts
since Nov 29, 2011 
from

How to save multiple selected list items in database and dropdowns list items?

  Posted on: 10 Feb 2012       
hi

can any one suggest me how to insert selected multiple list items to database and dropdowns list items?
Satyapriya Nayak
posted  2264 posts
since  Mar 24, 2010 
from 

 Re: How to save multiple selected list items in database and dropdowns list items?
  Posted on: 10 Feb 2012        0  
Hi Fahad,


Try this...


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication9.WebForm1" %>

<!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>
   
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
    </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 WebApplication9
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string str;
        SqlCommand com;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.AutoPostBack = true;
                DropDownList1.Items.Add("Select");
                DropDownList1.Items.Add("Raj");
                DropDownList1.Items.Add("Ravi");
                DropDownList1.Items.Add("Rahul");
                DropDownList1.Items.Add("Rajesh");

            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            for (int i = 0; i < DropDownList1.Items.Count; i++)
            {
                if (DropDownList1.Items[i].Selected == true)
                {
                    str = "insert into employee values('" + DropDownList1.Items[i].ToString() + "')";
                    com = new SqlCommand(str, con);
                    com.ExecuteNonQuery();
                    Response.Write("<script>alert('Items Inserted');</script>");
                }
            }
        }
    }
}



Thanks
If this post helps you mark it as answer
fahad sheikhji
posted  22 posts
since  Nov 29, 2011 
from 

 Re: How to save multiple selected list items in database and dropdowns list items?
  Posted on: 10 Feb 2012        0  
hi in my project m binding list items from sql like for example
1listbox and items like[tiger,lion,cat,dog(this values from one particular column from sql)]when i select it should show in another listbox tats 2listbox[lion,dog,cat]
when i click save it should insert into sqltable.

Satyapriya Nayak
posted  2264 posts
since  Mar 24, 2010 
from 

 Re: How to save multiple selected list items in database and dropdowns list items?
  Posted on: 10 Feb 2012   Accepted Answer     1  
Hi Fahad,


Try this...


Select all the items from listbox2 and then click the insert button.

<%@ 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="Button5" runat="server" onclick="Button5_Click" Text="Insert" />
    </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)
            {
//                ListBox1.Items.Add("Choose");
                con.Open();
                str = "select * from  animal";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    ListBox1.Items.Add(reader["category"].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 Button5_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 animal1 values('" + ListBox2.Items[i].ToString() + "')";
                    com = new SqlCommand(str, con);
                    com.ExecuteNonQuery();
                  
                }
            }
            Response.Write("Inserted");
        }
    }
}




Thanks
If this post helps you mark it as answer
fahad sheikhji
posted  22 posts
since  Nov 29, 2011 
from 

 Re: How to save multiple selected list items in database and dropdowns list items?
  Posted on: 10 Feb 2012        0  
hi this is my code

 protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
   
    {
   
    
        for (int i = 0; i < lbAddedItems.Items.Count; i++)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["dbConnection"].ToString());
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_InsertWorkAllocation";

            SqlParameter pLID = new SqlParameter("@LID", SqlDbType.VarChar, 50);
            SqlParameter pBID = new SqlParameter("@BID", SqlDbType.VarChar, 50);
            SqlParameter pTID = new SqlParameter("@TID", SqlDbType.VarChar, 50);


            cmd.Connection = conn;
            Session["LID"] = ddlTesterName.SelectedItem.Text;
            Session["BID"] = ddlBuildNO.SelectedItem.Text;
          
            Session["TID"] = lbAddedItems.SelectionMode;
            string selectedItem = lbAddedItems.Items[i].Text;
          
            cmd.Parameters.AddWithValue("@LID", Session["LID"]);
            cmd.Parameters.AddWithValue("@BID", Session["BID"]);
            cmd.Parameters.AddWithValue("@TID", Session["TID"]);
            cmd.ExecuteNonQuery();

            cmd.Parameters.Clear();
         
        }
          

    }
this is my aspx page. when i selcet multiple records in secod lis box

like this saving to sql table i want to save in tid like test8,test6,test10.but its adding only 1..and if i select other testers also its adding only "Tester"..can any one help?


fahad sheikhji
posted  22 posts
since  Nov 29, 2011 
from 

 Re: How to save multiple selected list items in database and dropdowns list items?
  Posted on: 10 Feb 2012        0  
hi bro thanks..how to add in one table it contains 3 columns...two to add drop downs selected items and one for to add selected items how to give coding for that together??
       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved