Click here to Skip to main content
15,909,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listbox, I want every item in this box to be sent to a single cell, using commas to separate each item. I'm quite new to this, can anyone help. so far I have...



for (int i = 0; i < CustLB.Items.Count; i++)
{
    ListItem item = new ListItem();
    item.Text = CustLB.Items[i].Text;
    item.Value = CustLB.Items[i].Value;
    OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] = (@Loc)", MAcon);
    cmd.Parameters.AddWithValue("@Loc", ",");
    cmd.ExecuteNonQuery();

}


What I have tried:

string value = "";

for (int i = 0; i < CustLB.Items.Count; i++)
{
    if (value != "")
    {
        OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] = (@Loc)", MAcon);
        cmd.Parameters.AddWithValue("@Loc", value += ",");

    }
    value += CustLB.Items[i].Text;
}
// Now you have all the values in comma (,) separated string.

string[] arr = value.split(',');
Posted
Updated 12-Apr-18 16:48pm
Comments
BillWoodruff 12-Apr-18 14:27pm    
This appears to be part of another question, posted after this one. I suggest you edit them into one question.

My sense is that you need to spend some time learning the basics of use of data structures in WinForms, and C# programming in general.

You should avoid of using OR as a field name. It's reserved word[^] in most databases!

If you would like to build comma separated string value, check this:
C#
string commaVal = string.Join(",", CustLB.Items.Cast<object>()
			.Select(x=> x.ToString()));

OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] = @Loc", MAcon);
cmd.Parameters.AddWithValue("@Loc", commaVal ",");


Another way is to use StringBuilder[^].
Why? Because string[^] is immutable!

C#
//create new instance of StringBuilder
StringBuilder sb = new StringBuilder();
foreach(object o in CustLB.Items)
{
   //append string
   sb.Append(string.Format("{0},", o.ToString));
}
//finally
string loc = sb.ToString();
 
Share this answer
 
v3
To get a String[] of ListBox Items:
string[] lbItemsAsStrArray = listBox1.Items.Cast<string>().ToArray();
I wonder if you are using a ListView here, rather than a ListBox.
 
Share this answer
 
Comments
Member 13765884 12-Apr-18 14:54pm    
I'm using a listbox here
If I understand the question correctly, you're trying to build an IN list in the WHERE clause. If this is the case, perhaps something like

C#
OleDbCommand lct = new OleDbCommand("UPDATE [Or] SET [Loc] IN ", MAcon);
string inContent = "";

for (int i = 0; i < CustLB.Items.Count; i++) {
   inContent += $"@loc{i},";
   lct.Parameters.AddWithValue($"@loc{i}", CustLB.Items[i].ToString());
}
inContent = inContent.TrimEnd(',');
lct.CommandText += $"({inContent})";

Also as already pointed out, try to avoid reserved words in names.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900