Satyapriya Nayak
posted
2264 posts
since
Mar 24, 2010
from
|
|
Re: How to save multiple selected list items in database and dropdowns list items?
|
|
|
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
|
|
|
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="<|" onclick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="|>" onclick="Button2_Click" /> <asp:Button ID="Button3" runat="server" Text="<<|" onclick="Button3_Click" /> <asp:Button ID="Button4" runat="server" Text="|>>" 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?
|
|
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
|
|
|
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??
|
|
|
|
|
|