using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand sqlCommand1 = conn.CreateCommand())
{
object[] o = new object[listBox1.Items.Count];
foreach(object o in listBox1.Items)
string insertActivities = string.Format("INSERT into DATA (ID, ActivityName) VALUES ({0}, o)", DropDownList_EmployID.SelectedValue,);
sqlCommand1.CommandText = insertActivities;
sqlCommand1.CommandType = CommandType.Text;
conn.Open();
sqlCommand1.ExecuteNonQuery();
conn.Close();
}
}
17 Replies - 4061 Views - Last Post: 06 July 2010 - 05:02 AM
#1
listbox multiple items
Posted 01 July 2010 - 06:45 AM
Replies To: listbox multiple items
#2
Re: listbox multiple items
Posted 01 July 2010 - 07:06 AM
Second, what does your object array here do? You declare it but never use it. And then you enter the foreach loop, and declare another variable, this time just a plain object, with the same name. That shouldn't even work.
Third, what does your foreach loop do? You never use the object o variable. You use the string "o", but that's always going to be "o" no matter what
This post has been edited by insertAlias: 01 July 2010 - 07:07 AM
#3
Re: listbox multiple items
Posted 01 July 2010 - 07:16 AM
insertAlias, on 01 July 2010 - 09:06 AM, said:
Second, what does your object array here do? You declare it but never use it. And then you enter the foreach loop, and declare another variable, this time just a plain object, with the same name. That shouldn't even work.
Third, what does your foreach loop do? You never use the object o variable. You use the string "o", but that's always going to be "o" no matter what
I think she is using the "object o" in the foreach simply as a way to loop through the items in the ListBox. She doesn't have to worry about invalid casting since it will cast to an "object".
IrishGirl, are you running into an issue? The code would seem to work.
#4
Re: listbox multiple items
Posted 01 July 2010 - 07:20 AM
public partial class Form1 : Form
{
IList<User> users = new List<User>();
public Form1()
{
InitializeComponent();
PrepareTestData();
}
private void PrepareTestData()
{
User u1 = new User { Id = 1, ActivityName = "Activity 1" };
User u2 = new User { Id = 2, ActivityName = "Activity 2" };
User u3 = new User { Id = 3, ActivityName = "Activity 3" };
users.Add(u1);
users.Add(u2);
users.Add(u3);
usersListBox.DataSource = users;
usersListBox.DisplayMember = "ActivityName";
usersListBox.ValueMember = "Id";
}
private void btnInsertSelected_Click(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection selection = usersListBox.SelectedItems;
foreach (User user in selection)
{
//Database insert logic here...
}
}
}
class User
{
public int Id { get; set; }
public string ActivityName { get; set; }
}
#5
Re: listbox multiple items
Posted 01 July 2010 - 07:29 AM
eclipsed4utoo, on 01 July 2010 - 06:16 AM, said:
insertAlias, on 01 July 2010 - 09:06 AM, said:
Second, what does your object array here do? You declare it but never use it. And then you enter the foreach loop, and declare another variable, this time just a plain object, with the same name. That shouldn't even work.
Third, what does your foreach loop do? You never use the object o variable. You use the string "o", but that's always going to be "o" no matter what
I think she is using the "object o" in the foreach simply as a way to loop through the items in the ListBox. She doesn't have to worry about invalid casting since it will cast to an "object".
IrishGirl, are you running into an issue? The code would seem to work.
See, I don't think it will. Look at this line closely:
string insertActivities = string.Format("INSERT into DATA (ID, ActivityName) VALUES ({0}, o)", DropDownList_EmployID.SelectedValue,);
It's not using the value of object o, it's using the literal string "o". So every insert would be "o". The first parameter is coming from a different control altogether, what I'm assuming is an employee id selector.
OP would need to rewrite it more like this:
string insertActivities = "INSERT into DATA (ID, ActivityName) VALUES (@ID, @ActivityName)";
sqlCommand1.CommandText = insertActivities;
sqlCommand1.Parameters.AddWithValue("@ID", DropDownList_EmployID.SelectedValue);
sqlCommand1.Parameters.AddWithValue("@ActivityName", o);
Also, to irishgirl:
Some database advice. If those are all the fields in your table, I would suggest adding a row id field. Something like an auto-increment integer field to be your primary key. It is very much worth doing.
This post has been edited by insertAlias: 01 July 2010 - 07:31 AM
#6
Re: listbox multiple items
Posted 01 July 2010 - 08:18 AM
#7
Re: listbox multiple items
Posted 01 July 2010 - 08:24 AM
Quote
#8
Re: listbox multiple items
Posted 01 July 2010 - 08:38 AM
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand sqlCommand1 = conn.CreateCommand())
{
// object[] o = new object[ListBox_Activities.Items.Count];
foreach (object o in ListBox_Activities.SelectedValue)
{
string insertActivities = string.Format("INSERT into Data (ID, ActivityName) VALUES (@ID, @ActivityName)");
sqlCommand1.CommandText = insertActivities;
sqlCommand1.Parameters.AddWithValue("@ID", DropDownList_Employees.SelectedValue);
sqlCommand1.Parameters.AddWithValue("@ActivityName", o);
sqlCommand1.CommandType = CommandType.Text;
conn.Open();
sqlCommand1.ExecuteNonQuery();
conn.Close();
}
}
}
GridView_ABC.DataBind();
GridView_Team.Visible = false;
This post has been edited by eclipsed4utoo: 01 July 2010 - 11:48 AM
Reason for edit:: fixed code formatting
#9
Re: listbox multiple items
Posted 01 July 2010 - 11:46 AM
#10
Re: listbox multiple items
Posted 05 July 2010 - 12:42 AM
foreach(object o in ListBox_Activities.SelectedValue) {
and then completely skips the block of code there, this is why it's showing blank and not inserting hmmm.
#11
Re: listbox multiple items
Posted 05 July 2010 - 12:50 AM
irishgirl, on 05 July 2010 - 06:42 AM, said:
foreach(object o in ListBox_Activities.SelectedValue) {
and then completely skips the block of code there, this is why it's showing blank and not inserting hmmm.
SelectedValue isn't a collection. It's a single object.
#12
Re: listbox multiple items
Posted 05 July 2010 - 01:01 AM
foreach (ListItem li in ListBox_Activities.Items)
but this obviously entered every entry into the db, not just the several selected ones. How can I do it for the several selected? If I try as
foreach (object o in ListBox_Activities.SelectedValue)
It passes through blanks.
This post has been edited by irishgirl: 05 July 2010 - 01:08 AM
#13
Re: listbox multiple items
Posted 05 July 2010 - 01:16 AM
foreach (ListItem li in ListBox_Activities.Items) {
if (li.Selected)
{
//
}
}
#14
Re: listbox multiple items
Posted 05 July 2010 - 01:23 AM
This post has been edited by insertAlias: 05 July 2010 - 01:25 AM
#15
Re: listbox multiple items
Posted 05 July 2010 - 01:32 AM
|
|

New Topic/Question
Reply




MultiQuote




|