I have two arraylists. One contains filenames and the other contains failurecount. These values are from database tables in the code behind.
Now I want to add these two arraylists to one datatable. Is there a way to do this?
I have two arraylists. One contains filenames and the other contains failurecount. These values are from database tables in the code behind.
Now I want to add these two arraylists to one datatable. Is there a way to do this?
The information you provided is rather limited, so' I'll make a wild guess here.
var table = new DataTable();
table.Columns.Add("value1");
table.Columns.Add("value2");
for (int i = 0; i < arrayListOne.Count; i++)
{
var row = table.NewRow();
row["value1"] = arrayListOne[i];
row["value2"] = arrayListTwo[i];
table.Rows.Add(row);
}
Of course this would only work if both lists have the same length.
If this is not what you want you will have to add more information.
So many things wrong here. The first is, of course, that for .Net 2.0 and later you should not use ArrayLists anymore. Use generic List<T>
s instead. Secondly, and just as important, the best way to get these into the same datatable is to re-write the sql you used to generate them so that instead of two queries you only have one. Finally, you didn't share any code. How are we going to help you fix your problem if we can't see the code you're using?
Yes, there are many ways to add two ArrayList instances to one DataTable instance. Here is one way that shows how to add any number of ArrayLists to one DataTable using a complete code sample compiled to the .NET Framework 2.0.
Note: Other people are doing a good job trying decipher what you're up to - I'm just going to answer the question directly with a full code snippet in case you can gain any insights from it for yourself.
This really isn't a complex answer - it just sets up a couple of ArrayList instances first with sample data to pertain to your question and provides a snippet at the end to test the solution.
Please comment what insights you find in here, if any. Thank you.
namespace Com.StackOverflow {
using System.Diagnostics;
using System.Data;
using System.Collections;
public static class SOQuestion__Add_two_arraylists_in_a_datatable {
public static void AnswerQuestionDirectly() {
// - - - - - - - - Prelimary setup to question - - - - - - - -
// Sample list of filenames (3 elements in total).
ArrayList listFilenames = new ArrayList( new[] {@"C:\a.dat", @"C:\b.dat", @"C:\c.dat"} );
// Sample list of error counts (2 elements in total).
ArrayList listFailureCounts = new ArrayList( new[] {88,51} );
// - - - - - - A Direct answer to the question - - - - - - -
// Create DataTable structure with one column.
DataTable dTable = new DataTable();
dTable.Columns.Add("Column 1");
/* Populate DataTable with all lists at once.
* Note: keep appending
* to { array initialization list } all lists you want included
* in the DataTable instance.
*/
foreach (ArrayList currentList in new[] { listFilenames, listFailureCounts }) {
foreach (object element in currentList)
dTable.Rows.Add(new[] { element }); // one column per row
}
// - - - - - - - - Test it for expected counts - - - - - - - -
int totalExpected = listFilenames.Count + listFailureCounts.Count;
//Verify DataTable contains the same amount of rows.
Debug.Assert(dTable.Rows.Count == totalExpected);
}
}
}
The above answer can be copied and pasted into a .cs file and run with the following code: using Com.StackOverflow;
.
SOQuestion__Add_two_arraylists_in_a_datatable.AnswerQuestionDirectly();