I am fetching data from sql server and exporting it to excel programmatically using c#.Based on the column data type, I am doing format of cell.Here I am facing one issue.
In sql server,retrieving column data type is defined as numeric(20,0).In my application column type is coming as Decimal.I want to get the type other numeric type like Double or Int64. Is there any way to get it?
Since the datatype in the DB is numeric, you have two options to get either a double or int.
Change the datatype in the database from numeric to int.
Receive the data as a decimal and round it. For this, use Math.Round as Convert.ToInt32 can have some unexpected behavior.
Related
I am trying to retrieve the exact value that I see in the database.
The database column type is float. The database is not mine and I cannot change its type or anything. In the database, I have the value 0.1, but when I read it with SqlDataReader.GetDouble it returns something like 0.09999.....98.
I also tried using GetValue and then manually converting to double, but still no difference.
I also cant use static rounding because while this value is 0.1 in the database and is read as 0.09...98, there are some values that have a lot more decimals. F.e. there is a value 0.0151515151515152 which with GetDouble is being read as 0.015151515151515148
I am trying to map bigint data from sq server table to c# long variable. Which I believe is the correct way to map.
long id = (long)ds.Tables[2].Rows[0].ItemArray[0];
I also tried below as suggested on SO.
long id = (long)(double)ds.Tables[2].Rows[0].ItemArray[0];
With both above I get below error:
System.InvalidCaseException : Specified cast is not valid.
In case you are wondering what data it contains, it is "1".
Convert.toInt64 got it worked. Thanks all
You are just hiding a bigger problem you have. The datatype in your column of your c# DataTable object held within the DataSet ds is likely set incorrectly. You need to set your table up so that the column's property for DataType is set to be Int64.
I've an Oracle table with a column of type NUMBER(38,0)
I need to fetch this column to my C# application.
I use the library System.Data.Odbc.OdbcDataReader to read the data from my Oracle table.
I've tried fetching the data by using the normal functions like:
var data = oracleReader["COLUMN"].ToString();
And
var data = oracleReader.GetString(0);
And even the oracleReader.GetBytes() function.
But I always get System.OverflowException, because the OdbcDataReader always try to get the column as decimal in the last step:
System.OverflowException: Value was either too large or too small for a Decimal.
at System.Data.Odbc.OdbcDataReader.internalGetDecimal(Int32 i)
at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i, TypeMap typemap)
at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i)
at System.Data.Odbc.DbCache.AccessIndex(Int32 i)
at System.Data.Odbc.OdbcDataReader.internalGetString(Int32 i)
at System.Data.Odbc.OdbcDataReader.GetString(Int32 i)
I'm happy if I can get this data as a String to my Application.
FYI, I can not change the datatype of the column, I need to work with this.
This data type is an alias for the NUMBER(38) data type, and is designed so that the OracleDataReader returns a System.Decimal or OracleNumber instead of an integer value. Using the .NET Framework data type can cause an overflow.
Come to think of it you actually need BigInteger to be able to represent the same number of significant digits as to what NUMBER defaults to. I've never seen anyone do that and I would suppose it's a very rare need. Also BigInteger still wouldn't cut it since NUMBER can be of positive and negative infinity.
You can use this list for future research.
I have an Excel 2000 file with around 25 columns. In column18 the column name is "MONTH" and its data type is Date Time. The value is displayed as (1/5/2009) in format(dd/mm/yyyy).
I need to change its data type to string and display the data in this format: (01-05-2010).
Can anyone tell me how to change the data type and change the way we display the data?
read the cell value
depending on a number of factors, the datatype in .net will either be DateTime, string, or double.
if type is not DateTime, then convert to DateTime. If string, then simply DateTime.Parse(), if double then use function here
Convert DateTime to string with desired format. DateTime.ToString("yyyy'-'MM'-'dd"). This is a good resource for formatting dates.
Set the cell's value to this string value.
In the program I'm currently working on, my table has a Create_Timestamp column, which I defined as timestamp.
When I'm working with my data context and my form values in my controller on the HttpPost, I'm trying the following:
NewsArticle article = new NewsArticle();
article.Create_Timestamp = System.DateTime.Now;
The error I get is Cannot implicitly convert from 'System.DateTime' to 'System.Data.Linq.Binary'
I've tried to force the conversion, but I'm unsure exactly what I'm doing at this point.
Is it possible in C# to do this conversion and still have Linq be happy with me?
Thanks
I am guessing you are using the SQL timestamp type in your table and you are expecting it to be a DateTime. Timestamp isn't really meant for holding Date/Time information. From MSDN (http://msdn.microsoft.com/en-us/library/aa260631(SQL.80).aspx):
timestamp is a data type that exposes
automatically generated binary
numbers, which are guaranteed to be
unique within a database. timestamp is
used typically as a mechanism for
version-stamping table rows. The
storage size is 8 bytes.storage size is 8 bytes.
Change your column "Create_Timestamp" to DateTime and you should be fine.