0

I have a dataset with one datatable, one row and six columns:

DataSet holdall = new DataSet();

            holdone.Fill(holdall);

            DataTable invoiceTable = holdall.Tables["INVOICE"];

The data comes from an SQL database, and consists of several different data types. I need to pass this data into an array or a list (AFAIK An array is more appropriate), and on the way convert it into string format:

string[] currentInvoice = new string[6];
currentInvoice = invoiceTable.AsEnumerable().Take(1).ToString().ToArray();

This gives me the following error:

Cannot implicitly convert type 'char[]' to 'string[]'

I have also tried

foreach (DataRow INVOICE in invoiceTable.AsEnumerable())
            {
                currentInvoice.ToArray();
            }

However, this returns a Null exception, that leads be to believe that there is no data in the datatable. Upon examining the datatable via a stop point, I find that there is the correct amount of data.

Am I overcomplicating this?

4
  • What is the original data and what is the desired data? Can you clarify it? Show an example of your table and the desired array. Commented Jun 26, 2014 at 13:31
  • For security purposes, I cannot give you any actual data (I am working with sensitive information pertaining to a GFO). What I can say is that I have SQL types: Int, Char, Money and DateTime. I need all of this in string format. Commented Jun 26, 2014 at 13:33
  • Just give some example like : {10, "asdas", 10.0}, {12312, "asdf", 11.123123} ... and the resulting items in the array like: "10,asdas,10.0", "123.... Commented Jun 26, 2014 at 13:37
  • @EugenePodskal Table: {09454, 097656, name, £99.99, £5.99, 01/01/2011} The resulting array should look exactly the same, but all entries in string "format". Commented Jun 26, 2014 at 13:39

1 Answer 1

4

When you call .ToString() you're converting whatever that is to a single string. Calling .ToArray() on a single string doesn't split that string into any meaningful substrings, it just converts it to a char[], which as the error states is incompatible with the variable type string[].

So, if I understand correctly... You have a table, of which you want only the first row, and you want the items in that row to be strings in an array? Something like this ought to work:

currentInvoice = invoiceTable.AsEnumerable().First().ItemArray.Select(i => i.ToString()).ToArray();

It takes the table as an enumerable, grabs the first row, looks at the array of items in that row, converts each item to a string, and converts that enumeration of strings into an array.

Edit: Note the use of .First() instead of .Take(1). While conceptually they do the same thing, .Take() returns a collection of items (even if there's only one item in the collection) whereas .First() returns only a single item. Note also that these could throw errors if there are no items in the collection from which you try to extract the first element. So you might want to add some error checking to ensure the table has rows before trying to extract the first row, if there could be a situation where it doesn't have rows.

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

7 Comments

This is precisely what I need. As a matter of fact, there is only one row in one table, as this is a direct query to find one particular record.
I spoke too soon: 'System.Collections.Generic.IEnumerable<System.Data.DataRow>' does not contain a definition for 'ItemArray' and no extension method 'ItemArray' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Data.DataRow>' could be found.
@Rote: Sorry, replace .Take(1) with .First() instead. I'll update the answer shortly...
Null exception: Value cannot be null. Does this imply that there is no data? Or that there is something wrong with getting the data to the DataTable?
@RoteKatze: When you debug this, what's null?
|

Your Answer

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