473,378 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

System.InvalidCastException: Specified cast is not valid

I keep geting the exception:

System.InvalidCastException: 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.SetValues(dr);
Here is part of the class that contains the method that I want to call:

using System;
using System.Data;
using System.Collections;

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_operator;

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

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

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

Trace.LeaveConst("Policy(dr)");
}

public void SetValues(DataRow dr)
{
_qid = (int)dr["qid"];
_cid = (int)dr["cid"];
_policy_number = dr["policy_number"].ToString();
_inception_date = dr["inception_date"].ToString();
_expiry_date = dr["expiry_date"].ToString();
_raw_premium = (float)dr["raw_premium"];
_policy_fee = (float)dr["policy_fee"];
_admin_fee = (float)dr["admin_fee"];
_ipt = (float)dr["ipt"];
_total_premium = (float)dr["total_premium"];
_is_renewal = (bool)dr["is_renewal"];
_operator = dr["operator"].ToString();
_date_quoted = dr["date_quoted"].ToString();
_on_cover = (bool)dr["on_cover"];
_on_cover_date = dr["on_cover_date"].ToString();
_on_cover_operator = dr["on_cover_operator"].ToString();
}
}
The code that instantiates the class and calls the method is in here:

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

policy.SetValues(dr);

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

System.InvalidCastException: 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.SetValues(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.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2


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

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

Nov 13 '05 #3
Floela <fl****@benjamin.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.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #4

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

Similar topics

3
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...
2
by: Michael Moreno | last post by:
Hello, I am failing to understand why I get an invalid cast exception with this very simple code. I have derived the EventArgs class as follow: public class StringArgs : System.EventArgs {...
1
by: Arjen | last post by:
Hello, Does somebody haves a solution for this? Thanks! Line 74: if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
1
by: Miguel Angel Dinelli | last post by:
I´m trying to execute a project and I get this error System.InvalidCastException: Specified cast is not valid. I found in microsoft some Q in 327287, but I not sure how make to solution it. ...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
0
by: Miriam | last post by:
Hello, I am trying to edit an xml file. I am using Visual Studio .NET 2003. When I click on the "Data" tab (instead of the XML tab) to enter in data like in excel, the following occurs: If I...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
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...
8
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...
4
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.