[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1731
  • Last Modified:

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

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
1E+13

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"
System.Data.
IDataReader IReader = objConn.ExecuteReaderII("SECURITY", this.sQuery, true);
System.Data.
 
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);
 
else
 
Cache[
 
"report"] = obj;
}

Open in new window

0
techbie
Asked:
techbie
1 Solution
 
anyoneisCommented:
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));
            dt.Columns.Add(dc);
            dc = new DataColumn("Value", typeof(double));
            dt.Columns.Add(dc);
            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");
            dt.Columns.Add(dc);
        }
    }
}

Open in new window

0
 
techbieAuthor Commented:
Thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now