473,836 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.InvalidC astException: Specified cast is not valid

I keep geting the exception:

System.InvalidC astException: Specified cast is not valid.
I get this when I try to pass a DataRow into an object method. The line that
causes this error is:

policy.SetValue s(dr);
Here is part of the class that contains the method that I want to call:

using System;
using System.Data;
using System.Collecti ons;

namespace IDUL.Policies
{

/// <summary>
/// </summary>
public class Policy
{
private int _qid;
private int _cid;
private string _policy_number;
private string _inception_date ;
private string _expiry_date;
private float _raw_premium;
private float _policy_fee;
private float _admin_fee;
private float _ipt;
private float _total_premium;
private bool _is_renewal;
private string _operator;
private string _date_quoted;
private bool _on_cover;
private string _on_cover_date;
private string _on_cover_opera tor;

/// <summary>
/// </summary>
public Policy()
{
Trace.Construct or("Policy()") ;
}

/// <summary>
/// </summary>
public Policy(DataRow dr)
{
Trace.EnterCons t("Policy(dr)") ;

if (dr != null)
{
SetValues(dr);
}

Trace.LeaveCons t("Policy(dr)") ;
}

public void SetValues(DataR ow dr)
{
_qid = (int)dr["qid"];
_cid = (int)dr["cid"];
_policy_number = dr["policy_num ber"].ToString();
_inception_date = dr["inception_date "].ToString();
_expiry_date = dr["expiry_dat e"].ToString();
_raw_premium = (float)dr["raw_premiu m"];
_policy_fee = (float)dr["policy_fee "];
_admin_fee = (float)dr["admin_fee"];
_ipt = (float)dr["ipt"];
_total_premium = (float)dr["total_prem ium"];
_is_renewal = (bool)dr["is_renewal "];
_operator = dr["operator"].ToString();
_date_quoted = dr["date_quote d"].ToString();
_on_cover = (bool)dr["on_cover"];
_on_cover_date = dr["on_cover_d ate"].ToString();
_on_cover_opera tor = dr["on_cover_opera tor"].ToString();
}
}
The code that instantiates the class and calls the method is in here:

foreach (DataTable dt in dsPolicies.Tabl es)
{
foreach (DataRow dr in dt.Rows)
{
Policy policy = new Policy();

policy.SetValue s(dr);

arrPolicies.Add (policy);
}
}
Any ideas what is causing this exception?
Nov 17 '05 #1
3 3812
Floela <fl****@benjami n.com> wrote:
I keep geting the exception:

System.InvalidC astException: Specified cast is not valid.
I get this when I try to pass a DataRow into an object method. The line that
causes this error is:

policy.SetValue s(dr);


I'm sure that's one part of the stack, but I don't think that's going
to be the actual direct causal line. I suspect it's somewhere within
SetValues itself. Have you tried stepping through SetValues to see
where the exception is thrown?
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 17 '05 #2


I get this when I try to pass a DataRow into an object method. The line that causes this error is:

policy.SetValue s(dr);
Inside SetValues there are many cast without checking,
i wonder if they are all safe ???
public void SetValues(DataR ow dr)
{
_qid = (int)dr["qid"];
_cid = (int)dr["cid"];
_raw_premium = (float)dr["raw_premiu m"];
_policy_fee = (float)dr["policy_fee "];
_admin_fee = (float)dr["admin_fee"];
_ipt = (float)dr["ipt"];
_total_premium = (float)dr["total_prem ium"];
_is_renewal = (bool)dr["is_renewal "];
_on_cover = (bool)dr["on_cover"];
}

Nov 17 '05 #3
Floela <fl****@benjami n.com> wrote:
I'm sure that's one part of the stack, but I don't think that's going
to be the actual direct causal line. I suspect it's somewhere within
SetValues itself.


SetValues is pretty simple - just assigning the values from the DataRow to
private variables within the class (see my original post). There's nothing
glaring as to where the problem could be within this method.


Yes there is - you've got lots of casts in SetValues. I'm sure it'll be
one of those which is failing.
Have you tried stepping through SetValues to see
where the exception is thrown?


No I haven't, as I'm actually coding the application using TextPad, not
VS.NET.


Ah. That's a pain. Can you print out a stack trace to show exactly
where it's failing?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 17 '05 #4

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

Similar topics

3
8234
by: karunakar | last post by:
Hi All Here iam getting this Error "Specified cast is not valid." My Project has Onc solution diffrent class libarary In that solution DALfactory solution iam getting this error I was calling this function in DALFactory class library(this library generate the DAL.dll) Another class library has this function public SecurityInfo Login(string username, string password) {
0
623
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is not valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> System.InvalidCastException: Specified...
8
4275
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is not valid"). How do I fix it ? using System.Diagnostics; .. .. class NewProcess: Process {
4
1847
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it says ProcessRequestMain... +3742?? Here's the error; Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in...
2
2508
by: thithi | last post by:
Server Error in '/Assignment' Application. -------------------------------------------------------------------------------- Specified cast is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Specified cast is not valid. Source...
0
9657
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10532
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
10577
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
10243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9359
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...
1
7775
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
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
2
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.