0

I have cruised through many questions now to get a solution to my problem but I really can't find any!

I am doing integrations toward ERP systems and encounter the same problem regularly so now it's time to do something about it.

Many SOAP webservices return query result as System.Data.DataSet, Where the dataset has 1 table.

The problem is that the specification I get might say that a date value is of datatype DateTime, but my program doesn't really agree.

Is there a way to get the specific data types of each column in a DataRow?

I have created a UnitTest to test my extension function, GetColumnTypes(DataTable Table).

DataTable tbl = new DataTable("TestTable");
tbl.Columns.Add("INTEGER");
tbl.Columns.Add("STRING");
tbl.Columns.Add("DATETIME");
tbl.Columns.Add("DOUBLE");
tbl.Columns.Add("FLOAT");
tbl.Columns.Add("LONG");

tbl.Rows.Add(int.MaxValue, string.Empty, DateTime.MaxValue, double.MaxValue, float.MaxValue, long.MaxValue);

This is just a dummy table but should serve the purpose. And my extension method.

    public static Type[] GetColumnTypes(DataTable Table)
    {
        IList<Type> types = new List<Type>();

        DataRow row = Table.Rows[0];

        foreach (object item in row.ItemArray)
        {
            types.Add(item.GetType());
        }

        return types.ToArray();
    }

The result here is string on each item in the row array. Even though I send in specific primitive data types in the row, eg. long.MaxValue etc.

If I try to fetch Table.Columns and then use DataType I only get string as well.

Is there a way to do this?

Thanks :)

1 Answer 1

1

While adding a coloumn to DataTable it decides the type of data what it is going to hold other wise it takes it as a string default. so while adding column do below modificaiton then it gives you expected result

DataTable tbl = new DataTable("TestTable");
        tbl.Columns.Add("INTEGER",typeof(int));
        tbl.Columns.Add("STRING", typeof(string));
        tbl.Columns.Add("DATETIME", typeof(DateTime));
        tbl.Columns.Add("DOUBLE", typeof(double));
        tbl.Columns.Add("FLOAT", typeof(float));
        tbl.Columns.Add("LONG", typeof(long));

        tbl.Rows.Add(int.MaxValue, string.Empty, DateTime.MaxValue, double.MaxValue, float.MaxValue, long.MaxValue);

        GetColumnTypes(tbl);
Sign up to request clarification or add additional context in comments.

4 Comments

Is this you are looking for ?
I will try it out! It sounds reasonable so I will first do a UnitTest and then do it towards a Webservice that is out of my control. Hopefully it is common to add typeof(type) when doing datatables :) Thanks!
Works like a charm on my est where I create the table. Now to the customer environment!
And it also works toward the ERP im tryingh to integrate with! Thanks alot :)

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.