SqlDataReader::GetValue problems

P

Prem

This is what I am trying to do
double utmx = (double)
(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));

and this is the exception that I get

System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="pdmssdeUpdate"
StackTrace:
at pdmssdeUpdate.updateSde.Main() in C:\Prem\PDMS
\C#PdmsSdeUpdate\pdmssdeUpdate\pdmssdeUpdate\updateSde.cs:line 123
at System.AppDomain.nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

If I do

Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));

I get

516098


Someone please help
 
D

David Browne

Prem said:
This is what I am trying to do
double utmx = (double)
(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));

and this is the exception that I get

System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="pdmssdeUpdate"
StackTrace:
at pdmssdeUpdate.updateSde.Main() in C:\Prem\PDMS
\C#PdmsSdeUpdate\pdmssdeUpdate\pdmssdeUpdate\updateSde.cs:line 123
at System.AppDomain.nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

If I do

Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));



But what is the type of the object returned from GetValue

Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")).GetType().Name);?David
 
P

Prem

The type is Int32

Thats what I get when I put in that line

This is what I am trying to do
double utmx = (double)
(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));
and this is the exception that I get
System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="pdmssdeUpdate"
StackTrace:
at pdmssdeUpdate.updateSde.Main() in C:\Prem\PDMS
\C#PdmsSdeUpdate\pdmssdeUpdate\pdmssdeUpdate\updateSde.cs:line 123
at System.AppDomain.nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));

But what is the type of the object returned from GetValue

Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")).GetType().Name);?David
 
P

Prem

Ok I figured it out, you have to use
double utmX =
Convert.ToDouble(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));

it will not do a cast

Thanks David

The type is Int32

Thats what I get when I put in that line

This is what I am trying to do
double utmx = (double)
(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));
and this is the exception that I get
System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="pdmssdeUpdate"
StackTrace:
at pdmssdeUpdate.updateSde.Main() in C:\Prem\PDMS
\C#PdmsSdeUpdate\pdmssdeUpdate\pdmssdeUpdate\updateSde.cs:line 123
at System.AppDomain.nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
If I do
Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));
But what is the type of the object returned from GetValue
Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")).GetType().Name);?David
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Prem said:
The type is Int32

Thats what I get when I put in that line

This is what I am trying to do
double utmx = (double)
(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));
and this is the exception that I get
System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="pdmssdeUpdate"
StackTrace:
at pdmssdeUpdate.updateSde.Main() in C:\Prem\PDMS
\C#PdmsSdeUpdate\pdmssdeUpdate\pdmssdeUpdate\updateSde.cs:line 123
at System.AppDomain.nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
If I do
Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));
But what is the type of the object returned from GetValue

Console.WriteLine(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")).GetType().Name);?David

What you are doing is not really casting, but unboxing. The GetValue
method returns an object that contains the data, and getting the value
out of the object is called unboxing it.

When unboxing a value, you have to use the actual data type that the
object is containing. Therefore you have to unbox it using the int data
type, then you can cast it to a double:

double utmx =
(double)(int)(pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83")));

Or you can use the GetInt32 method instead, so that you don't have to
unbox the value:

double utmx =
(double)(pSqlReader.GetInt32(pSqlReader.GetOrdinal("UTM_X83")));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top