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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 10, 2012, 5:01 AM
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
fahad sheikhjiPosted Feb 10, 2012, 11:55 PM
fahad sheikhjiPosted Feb 10, 2012, 7:10 AM
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 sheikhjiPosted Feb 10, 2012, 3:34 AM
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 NayakPosted Feb 10, 2012, 2:26 AM
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