0

method1:

public void method1(DataTable ServerGroupIds)
{
   obj.method2(ServerGroupIds);  
}

method2 :

public static void method2(string[] servergroups)
{
    obj.Message = userName + " has Un-Restricted the Project.";
}

Now I want to pass the DataTable values into the method2 String[] servergroups How can I pass my DataTable values into an array of string values?

Sorry, Forget to mention I have 2 columns in my DataTable. !st column is ProjectId and Second Column is Server Group Id. Now I need only ServerGroup Id's in my array of string

2
  • You don't use servergroups in second method Commented Mar 14, 2014 at 6:56
  • Can you tell us more about your datatable? Is it 1 column and N number of rows (1xN)? or is it Many columns and N number of rows (MxN)? Commented Mar 14, 2014 at 6:58

2 Answers 2

2

Try this

public void method1(DataTable ServerGroupIds)
{

   string [] serverGroups = ServerGroupIds.AsEnumerable().Select(t => t.Field<string>("ID")).ToArray<string>();

obj.method2(serverGroups );  

}

Don't forget to include System.Linq in t.Field<string>("ID"). Replace "ID" with the name of the column in the data table you want to put into the array

Sign up to request clarification or add additional context in comments.

Comments

0

For a single row you can do this:

var rowAsString = string.Join(", ", ServerGroupIds.Rows[0].ItemArray); 

Now add all the rows (by looping through your DataTable) rowAsString to a list:

List<string> list = new List<string>();
for (int i = 0; i < ServerGroupIds.Rows.Count; i++)
{
    string rowAsString = string.Join(", ", ServerGroupIds.Rows[i].ItemArray);
    list .Add(rowAsString );
}
string[] array = list.ToArray();

And pass to method2:

obj.method2(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.