We help IT Professionals succeed at work.

Change the Data type in the Data Set so that Excel sheet population works fine

techbie asked
Hi experts,

I was asked to create a report in Excel. I should take data from a table and populate it on Excel.

I used the below query
select * from database_table;
Now in database there is a column name "ID" and its data type is  number.

Since some of the ID data is large, and after populating them in excel, they get converted to some different representation.

for example the value 9999999999999 in excel becomes

I am asked to change the data type of this value to String in .net end, i.e they dont want me to use "select to_char(ID),.... from database_table". They wanna keep select * from database_table. but need to change the data type for column to string.

Any idea how do I do this. Is there a property like dataset.column.datatype = text

I think we can do it by changing the column property of the excel sheet to "General" or "Text".
But can this be done using .net side

Thanks very much,

this.sQuery="select * from database_table"
IDataReader IReader = objConn.ExecuteReaderII("SECURITY", this.sQuery, true);
DataSet obj = new System.Data.DataSet ();
obj.Load(IReader, System.Data.
LoadOption.OverwriteChanges, new string[] { "available" });
                                                      //need to change this obj dataset's ID column to string.
if (obj.Tables.Count > 0)
if (!Cache.ContainsKey("report"))
Cache.Add("report", obj);
"report"] = obj;

Open in new window

Watch Question

Software Developer
Top Expert 2006
You can't change the type of a column once it is in a DataT.able. I think the easiest solution is to change the select so that you convert the ID to a string - which means you can't do the "Select *", but must name the columns.

If they can tolerate a new column, say, "StringID", then all you have to do is add a column of type string to the datatable, and set it to an expression.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ReplaceColumn
    class Program
        static void Main(string[] args)
            // Simulate the creation of the dataset
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("Id", typeof(int));
            dc = new DataColumn("Value", typeof(double));
            dt.Rows.Add(1, 1.1);
            dt.Rows.Add(2, 2.2);
            dt.Rows.Add(3, 3.3);
            // OK, now that we have a dataset with data, add a column 
            dc = new DataColumn("IdAsString", typeof(string), "Id");

Open in new window



Explore More ContentExplore courses, solutions, and other research materials related to this topic.