how to select multiple values and save in database

Posted by Srinunelli under ASP.NET on 2/6/2012 | Points: 10 | Views : 2434 | Status : [Member] | Replies : 2
public partial class Move : System.Web.UI.Page
{
public void btnLeftTransfer_Click(object sender, EventArgs e)
{
int count = listBox1.Items.Count;//assigning items in listbox it into count variable

if (count != 0)//checking the conditions
{
for (int i = 0; i < count; i++)
{
listBox2.Items.Add(listBox1.Items[i]);
}
}
listBox1.Items.Clear();//clear the listbox after transfering records.
}
description:
it is not select multiple values and not save in database....please help me....



public void btnRightTransferAll_Click(object sender, EventArgs e)
{

int count = listBox2.Items.Count;

if (count != 0)
{

for (int i = 0; i < count; i++)
{

listBox1.Items.Add(listBox2.Items[i]);

}

}

listBox2.Items.Clear();

}


//code to transfer records from listbox1 to Listbox2 on select records..
public void btnSingleSelectTransfer_Click(object sender, EventArgs e)

{

//Checking Conditions of selection of listbox.

if (listBox1.SelectedIndex > -1)

{
listBox2.Items.Add(listBox1.SelectedItem);

listBox1.Items.Remove(listBox1.SelectedItem);
}

}

//code to transfer records from listbox2 to listbox1 on slect records.

public void btnSingleRightTransfer_Click(object sender, EventArgs e)
{

if (listBox2.SelectedIndex > -1)
{

listBox1.Items.Add(listBox2.SelectedItem);

listBox2.Items.Remove(listBox2.SelectedItem);

}

}

}




Responses

Posted by: Blessyjees on: 2/6/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

did you set the property "SelectionMode= Multiple" of the listbox. When do you need to update the database.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Srinunelli, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lalji_Mer on: 5/1/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi..

you can try this code sample.
First declare the namespaces

[code]

private string GetConnectionString()

{

//Where DBConnection is the connetion string that was set up in the web config file

return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;

}



private void InsertRecords(StringCollection sc)

{

SqlConnection conn = new SqlConnection(GetConnectionString());

StringBuilder sb = new StringBuilder(string.Empty);

foreach (string item in sc)

{

const string sqlStatement = "INSERT INTO Table1 (Employees) VALUES";

sb.AppendFormat("{0}('{1}'); ", sqlStatement, item);

}



try

{

conn.Open();

SqlCommand cmd = new SqlCommand(sb.ToString(), conn);

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);

}



catch (System.Data.SqlClient.SqlException ex)

{

string msg = "Insert Error:";

msg += ex.Message;

throw new Exception(msg);

}

finally

{

conn.Close();

}

}



protected void Page_Load(object sender, EventArgs e)

{



}

protected void Button1_Click(object sender, EventArgs e)

{

StringCollection sc = new StringCollection();



foreach (ListItem item in ListBox1.Items)

{

if (item.Selected)

{

sc.Add(item.Text);

}

}

InsertRecords(sc);

}

[/code]

LP MER

Srinunelli, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response