hi
can any one suggest me how to insert selected multiple list items to database and dropdowns list items?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 10, 2012, 12:01 AM EST
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" %>
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
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 9, 2012, 9:26 PM EST
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication9.WebForm1" %>
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("");
}
}
}
}
}
Thanks
If this post helps you mark it as answer
fahad sheikhji50 reputationPosted Feb 9, 2012, 10:34 PM EST
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.
fahad sheikhji50 reputationPosted Feb 10, 2012, 2:10 AM EST
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 sheikhji50 reputationPosted Feb 10, 2012, 6:55 PM EST