Why can't I do this??

  • Thread starter Thread starter Siv
  • Start date Start date
S

Siv

Hi,
This is driving me nuts, I have executed a SQL Stored Procedure that
populates a SQLDataReader (dr) and am reading in some fields that contain
numbers into a Struct that I have defined as follows:

=========================================
public struct KPIData
{
public byte Cond;
public Single Min;
public Single Max;
public Single Points;
public byte Band;
}

// I then instantiate an array called CSP with space for 11 items:

KPIData[] CSP;

CSP=new KPIData[10];

// Database stuff executes Stored Procedure and populates the DataReader
// Then eventually I do:

while (dr.Read())
{

CSP[n].Cond = (byte) dr["CondType"];
CSP[n].Min = (Single) dr["csMin"];
CSP[n].Max = (Single) dr["csMax"];
CSP[n].Points = (Single) dr["Points"];
CSP[n].Band = (byte) dr["Band"];


n += 1;
z= n % 10;

if (z== 0)
Array.Resize(ref CSP, CSP.Length + 10);

}

=========================================
The lines:

CSP[n].Min = (Single) dr["csMin"];
CSP[n].Max = (Single) dr["csMax"];
CSP[n].Points = (Single) dr["Points"];

All cause Explicit Type Conversion errors yet the byte ones work as I would
expect??
Can anyone explain to a Newbie why this is a problem, I have tried
substituting Float instead of Single and it still errors just the same???
 
What is the datatype of the item in the row? Most likely it does not support
an explicit conversion to a single.

Cheers,

Greg
 
It's a "Decimal" in SQL Server.
If I can't use single/float could I use another data type in C# that would
receive the field value. I must admit I thought it was complaining because
the data reader field was classed as an "object"?

--
Siv
Martley, Near Worcester, United Kingdom.


Greg Young said:
What is the datatype of the item in the row? Most likely it does not
support an explicit conversion to a single.

Cheers,

Greg

Siv said:
Hi,
This is driving me nuts, I have executed a SQL Stored Procedure that
populates a SQLDataReader (dr) and am reading in some fields that contain
numbers into a Struct that I have defined as follows:

=========================================
public struct KPIData
{
public byte Cond;
public Single Min;
public Single Max;
public Single Points;
public byte Band;
}

// I then instantiate an array called CSP with space for 11 items:

KPIData[] CSP;

CSP=new KPIData[10];

// Database stuff executes Stored Procedure and populates the DataReader
// Then eventually I do:

while (dr.Read())
{

CSP[n].Cond = (byte) dr["CondType"];
CSP[n].Min = (Single) dr["csMin"];
CSP[n].Max = (Single) dr["csMax"];
CSP[n].Points = (Single) dr["Points"];
CSP[n].Band = (byte) dr["Band"];


n += 1;
z= n % 10;

if (z== 0)
Array.Resize(ref CSP, CSP.Length + 10);

}

=========================================
The lines:

CSP[n].Min = (Single) dr["csMin"];
CSP[n].Max = (Single) dr["csMax"];
CSP[n].Points = (Single) dr["Points"];

All cause Explicit Type Conversion errors yet the byte ones work as I
would expect??
Can anyone explain to a Newbie why this is a problem, I have tried
substituting Float instead of Single and it still errors just the same???
 
Greg,

I changed the Singles to Decimals and all is well. I was being confused by
the compiler error messages, it kept saying that dr["csMin"] was type
"object" so it threw me off on the wrong path!
Thankfully you have me back on it again, many thanks.


--
Siv
Martley, Near Worcester, United Kingdom.
Greg Young said:
What is the datatype of the item in the row? Most likely it does not
support an explicit conversion to a single.

Cheers,

Greg

Siv said:
Hi,
This is driving me nuts, I have executed a SQL Stored Procedure that
populates a SQLDataReader (dr) and am reading in some fields that contain
numbers into a Struct that I have defined as follows:

=========================================
public struct KPIData
{
public byte Cond;
public Single Min;
public Single Max;
public Single Points;
public byte Band;
}

// I then instantiate an array called CSP with space for 11 items:

KPIData[] CSP;

CSP=new KPIData[10];

// Database stuff executes Stored Procedure and populates the DataReader
// Then eventually I do:

while (dr.Read())
{

CSP[n].Cond = (byte) dr["CondType"];
CSP[n].Min = (Single) dr["csMin"];
CSP[n].Max = (Single) dr["csMax"];
CSP[n].Points = (Single) dr["Points"];
CSP[n].Band = (byte) dr["Band"];


n += 1;
z= n % 10;

if (z== 0)
Array.Resize(ref CSP, CSP.Length + 10);

}

=========================================
The lines:

CSP[n].Min = (Single) dr["csMin"];
CSP[n].Max = (Single) dr["csMax"];
CSP[n].Points = (Single) dr["Points"];

All cause Explicit Type Conversion errors yet the byte ones work as I
would expect??
Can anyone explain to a Newbie why this is a problem, I have tried
substituting Float instead of Single and it still errors just the same???
 
Siv said:
// I then instantiate an array called CSP with space for 11 items:

KPIData[] CSP;

CSP=new KPIData[10];

Although I doubt it is the problem this is only 10 items.
CSP[n].Min = (Single) dr["csMin"];
CSP[n].Max = (Single) dr["csMax"];
CSP[n].Points = (Single) dr["Points"];

All cause Explicit Type Conversion errors yet the byte ones work as I
would expect??
Can anyone explain to a Newbie why this is a problem, I have tried
substituting Float instead of Single and it still errors just the same???

Do this:

object x = dr["csMin"];
CSP[n].Min = (Single) x;

When it errors have a look at the value of x. Maybe it is null or string?

Michael
 
It's a "Decimal" in SQL Server.
If I can't use single/float could I use another data type in C# that would
receive the field value. I must admit I thought it was complaining because
the data reader field was classed as an "object"?

I think that when you have an object which is really a boxed decimal,
you can only cast (unbox) it to a decimal. Only then you can cast
(convert) it to an other type. So this will work:
float f = (float)(decimal)dr["csMin"];

Hans Kesting
 
Hans,
Wow, I am a VB.NET programmer converting some VB6 stuff to C# and although I
understand C# it isn't my main language, so I am finding this ability to do
type conversions quite an eye opener.

Thanks for your help.


--
Siv
Martley, Near Worcester, United Kingdom.

Hans Kesting said:
It's a "Decimal" in SQL Server.
If I can't use single/float could I use another data type in C# that
would receive the field value. I must admit I thought it was complaining
because the data reader field was classed as an "object"?

I think that when you have an object which is really a boxed decimal,
you can only cast (unbox) it to a decimal. Only then you can cast
(convert) it to an other type. So this will work:
float f = (float)(decimal)dr["csMin"];

Hans Kesting
 

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

Back
Top