Hi friends,
I want to pass a arryalist object to a method which inserts record to sql table. How do i add all the column names and values to this arraylist object and also check for the no of columns passed?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jean PaulPosted Nov 9, 2011, 3:01 AM
I can suggest some idea like:
1) Convert your object into an entity like Employee
public class Employee
{
public int Id;
public string Name;
}
2) Pass the employee object through ArrayList
ArrayList list = new ArrayList();
list.Add(new Employee() { Id = 1});
list.Add(new Employee() { Id = 2});
3) Invoke the Insert() method by specifying the properties assigned.. If you wanted to skip "Name", pass only "Id"
Insert(list, "Id");
Insert(ArrayList list, params string[] propertiesAssigned)
Nethra R SPosted Nov 9, 2011, 10:44 PM
I have not worked with arraylist and as you said, i have a class with 2 properties for column name and column value. But i do not know what to do next. I want to use the arraylist itself so can you guide further or give an example
And yes the arraylist will contain all the columns of a single row
Sam HobbsPosted Nov 9, 2011, 10:04 PM
There are many ways to do that. A simple solution is to use a structure or class that has at least two members; a name member and a value member and then make an ArrayList of instances of the structure or class. Another possibility is to use the DataRow class.