473,804 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't I do this??

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(re f 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
Martley, Near Worcester, United Kingdom.
Apr 26 '06 #1
6 1299
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" <dotnet@remove_ me.sivill.com> wrote in message
news:eW******** ******@TK2MSFTN GP03.phx.gbl...
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(re f 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
Martley, Near Worcester, United Kingdom.

Apr 26 '06 #2
Siv
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 [MVP]" <Dr************ *@hotmail.com> wrote in message
news:eF******** ******@TK2MSFTN GP05.phx.gbl...
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" <dotnet@remove_ me.sivill.com> wrote in message
news:eW******** ******@TK2MSFTN GP03.phx.gbl...
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(re f 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
Martley, Near Worcester, United Kingdom.


Apr 26 '06 #3
Siv
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 [MVP]" <Dr************ *@hotmail.com> wrote in message
news:eF******** ******@TK2MSFTN GP05.phx.gbl...
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" <dotnet@remove_ me.sivill.com> wrote in message
news:eW******** ******@TK2MSFTN GP03.phx.gbl...
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(re f 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
Martley, Near Worcester, United Kingdom.


Apr 26 '06 #4
"Siv" <dotnet@remove_ me.sivill.com> wrote in message
news:eW******** ******@TK2MSFTN GP03.phx.gbl...
// 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
Apr 26 '06 #5
> 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.


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
Apr 26 '06 #6
Siv
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" <ne***********@ spamgourmet.com > wrote in message
news:mn******** *************** @spamgourmet.co m...
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.


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

Apr 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2852
by: AK | last post by:
Hi all, I have a form which simulates a form collecting a person's info for a job. The form has the field's like fname, lname, address etc. The last field in the form is browse for resume and upload it. What I did until now is, I am inserting all the data other than the resume into a table and storing the resumes in a seperate directory. My questions are: 1) Instead of storing the resumes in a seperate directory, can I store...
1
3685
by: farzad | last post by:
Hi I try to install php_ming.so in my php . I am using Redhat 8.0 php 4.2.2 apache 2.0.4. I am doing as the howto install file want me to do. as follow. http://ming.sourceforge.net/install.html ------------------ download php_ming.so.gz uncompress it and copy it to your php modules directory
1
3150
by: Randell D. | last post by:
Folks, I use Apache/PHP with Webazlier to view the log files. I get a number of user agents (web browsers?) that visit the website - For example: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1 Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90) Mozilla/2.0 (compatible; T-H-U-N-D-E-R-S-T-O-N-E) Googlebot/2.1 (+http://www.googlebot.com/bot.html) Mozilla/3.0 (compatible; Indy Library)
0
1932
by: jeddiki | last post by:
I am reading up on this swfobject which uses javascript to embed the swf files. They say that I need to have adobe flash player 9.0 Well I was going to use Flowplayer. Does this mean that if I use swfobject then I can not use Flowplayer? Or am I getting mixed up ?
3
1720
by: Man4ish | last post by:
I am trying to create a table with three columns. first two columns will be having text values while last column has checkbox. How it can implemented? Any example doing the same will really help me. Thanks in advance.
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.