Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the contents for a datatable into an array

Status
Not open for further replies.

digitallyskilled

IS-IT--Management
Sep 23, 2004
39
US
I want to be able to get the items in a datatable into an array. The stored procedure populates the datatable. Ultimatly i will need to pass the array to another method that will extract the array and process the orderId.

public static int[] GetOrderId(int InvoiceId)
{
// Declare and initialize the return value
int [] orders = new int [0];
// Get the orders associated with an invoice
try
{
int @invoiceNo = InvoiceId;
DataSet ds = new DataSet();
string sqlConnection = ConfigurationSettings.AppSettings["SqlConnection"];
string spName = "getIOsByInvoice";

SqlParameter[] storedParams = new SqlParameter[0];
storedParams = SqlHelperParameterCache.GetSpParameterSet(sqlConnection, spName);
storedParams[0].Value = @invoiceNo;

ds = SqlHelper.ExecuteDataset(
sqlConnection,
CommandType.StoredProcedure,
spName,
storedParams);
DataTable dt = new DataTable();
dt = ds.Tables[0];

for(int i = 0; i < dt.Rows.Count; i++)
{
?????
}
}
catch
{
}
return orders;
}
 
Do you need the fields from the datatable in an array or the data from each row? Because if you need the data from the rows it's going to be a multi dimensional array.

If it is the data from each row in the datatable you may want to look at the ItemArray property of the DataRow.


If it's the fields or column name of the DataTable it'd be something like this.

foreach(DataColumn dc in myDataTable.Columns)
{
// Add each dc.ColumnName to an array
}

That's not exact but hopefully it helps put you in the right direction.
 
Sorry I just reread your question and it cleared a few things up.

First off you'll need to get the total count of order id's that will be in the array before you declare it.

Example:
-------------
// After the line
dt = ds.Tables[0];

// Declare your array
int [] orders = new int [dt.Rows.Count];

for(int i = 0; i < dt.Rows.Count; i++)
{
orders = Convert.ToInt32(dt.Rows["OrderIDFieldName"]);
}

It will look something like that. Sorry I'm in a hurry and I don't have time to elaborate. That approach may be off, but I think it's close.

Once again this code isn't exact, it might not run as written, but it should help get you close.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top