views:

755

answers:

3

I have a list box which is populated by this code:

  • lstOutcome.Items.Add(lstMap.SelectedItem.Text);

In the listbox lstOutcome, I need to be able to loop through the listbox and be able to take the value of the first,second, etc, list items.

The reason I need to loop through each line and grab the value of that line is so I can use whatever value was in that line for populating something else.

For example, in my list box I have:

  • 1
  • 2
  • 3

I need to be able to loop through the listbox on button click and have the values assigned to txtboxes:

  • textbox1.Text = 'item 1 in the listbox';
  • textbox2.Text = 'item 2 in the listbox';
  • textbox3.Text = 'item 3 in the listbox';

I am not sure if I need an array or how this can be accomplished. The point of this example is that I will actually be using the items in the listbox to map columns. I am importing an Microsoft Excel spreadsheet. In lstMap I have the column names and I am trying to get the column names to match my database. Using this code I am trying to take the values of the listbox:

foreach(object li in lstOutcome.Items)
{
    bulkCopy.DestinationTableName = "Customers";
    //Amount to bulkcopy at once to prevent slowing with large imports.
    bulkCopy.BatchSize = 200;
    SqlBulkCopyColumnMapping map1 = new SqlBulkCopyColumnMapping(li.ToString(), "CustomerID");
    bulkCopy.ColumnMappings.Add(map1);
A: 

You need to put the textboxes in an array, like this:

Textbox[] textboxes = new Textbox[] { textbox, textbox2, textbox3 };

for (int i = 0; i < listBox1.Items.Count; i++) {
    textboxes[i].Text = "Item: " + listBox1.Items[i].ToString();
}

Note that if there are more than three items in the listbox, this will fail because it will run out of textboxes. For a more complete solution, please add some context. What are the textboxes for? What is the data in the listbox coming from?

SLaks
ok I get what you're saying. I am going to do a little tweaking.
Joel
meh couldnt get it to work, cant interchange textbox with SqlBulkCopyColumnMapping
Joel
What do you mean? What error message do you get?
SLaks
A: 

I got it to work, check out this code amigo's

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString, options))
                    {
                        ArrayList myList = new ArrayList();
                        foreach (ListItem li in lstOutcome.Items)
                        {
                            myList.Add(li);
                        }
                        bulkCopy.DestinationTableName = "Customers";
                        //amount to bulkcopy at once to prevent slowing with large imports
                        bulkCopy.BatchSize = 200;
                        SqlBulkCopyColumnMapping map1 = new SqlBulkCopyColumnMapping(myList[0].ToString(), "CustomerID");
                        bulkCopy.ColumnMappings.Add(map1);
                        SqlBulkCopyColumnMapping map2 = new SqlBulkCopyColumnMapping(myList[1].ToString(), "Name");
                        bulkCopy.ColumnMappings.Add(map2);
                        SqlBulkCopyColumnMapping map3 = new SqlBulkCopyColumnMapping(myList[2].ToString(), "Address");
                        bulkCopy.ColumnMappings.Add(map3);
                        SqlBulkCopyColumnMapping map4 = new SqlBulkCopyColumnMapping(myList[3].ToString(), "City");
                        bulkCopy.ColumnMappings.Add(map4);
                        SqlBulkCopyColumnMapping map5 = new SqlBulkCopyColumnMapping(myList[4].ToString(), "State");
                        bulkCopy.ColumnMappings.Add(map5);
                        SqlBulkCopyColumnMapping map6 = new SqlBulkCopyColumnMapping(myList[5].ToString(), "ZipCode");
                        bulkCopy.ColumnMappings.Add(map6);
                        SqlBulkCopyColumnMapping map7 = new SqlBulkCopyColumnMapping(myList[6].ToString(), "Phone");
                        bulkCopy.ColumnMappings.Add(map7);
                        SqlBulkCopyColumnMapping map8 = new SqlBulkCopyColumnMapping(myList[7].ToString(), "Email");
                        bulkCopy.ColumnMappings.Add(map8);
Joel
You don't need to create an ArrayList. Instead, you can write `lstOutcome.Items[4].ToString()`. Also, you don't need a separate variable for each mapping. Instead, you can write `bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(...))`
SLaks
+1  A: 

A more efficient way to write your answer would be like this:

static readonly string[] FieldNames = new string[] { "CustomerID", "Name", "Address", ..., "Email" };

using(SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString, options)) {

    bulkCopy.DestinationTableName = "Customers";
    //amount to bulkcopy at once to prevent slowing with large imports
    bulkCopy.BatchSize = 200;

    for(int i = 0; i < FieldNames.Length; i++) {
        bulkCopy.ColumnMappings.Add(
            new SqlBulkCopyColumnMapping(lstOutcome.Items[i].ToString(), FieldNames[i])
        );
    }
}
SLaks