?
Solved

c# - SQLDataReader Cast Issues

Posted on 2011-10-24
3
Medium Priority
?
227 Views
Last Modified: 2012-05-12
Not a c# guru.  Just writing a quick script but getting issues.  See code box.

The code is part of a script that pulls SQL Server data and puts to Excel.

Keep getting cast errors.  Trying to account for that by checking the column type.

I need the code to be as dynamic as possible, so that it spits out any type of data, regardless if it's a date, integer, or string.


Here's my last error:

Unable to cast object of type 'System.Decimal' to type 'System.String'


Looking for a better what of doing???



for (int i = 0; i < dr2.FieldCount; i++)
    {

	
	if (dr2.GetDataTypeName(i).ToString() == "datetime")
	{
	    ws.Cells[row, col] = (dr2.GetDateTime(i).ToString("MM/dd/yyyy"));
	}
	else if (dr2.GetDataTypeName(i).ToString() == "int")
	{
	    ws.Cells[row, col] =dr2.GetInt32(i).ToString();
	}
	else
	{
	    ws.Cells[row, col] = dr2.GetString(i); ;
	}

	col++;
    }

Open in new window

0
Comment
Question by:carsRST
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37018659
Try changing line 14 from

ws.Cells[row, col] = dr2.GetString(i); ;

Open in new window


to

ws.Cells[row, col] = dr2[i].ToString();

Open in new window

0
 
LVL 16

Author Closing Comment

by:carsRST
ID: 37018675
Thanks, man!

I greatly appreciate your help and time.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37019262
NP, glad to help. Just be aware that that particular change will cause whatever type of object is housed in that column to have its default ToString method called. It may be of no consequence for the data you are working with, but it could come into play if one of the columns you were returning were of type xml on the database, for example.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question