System.InvalidCastException: Specified cast is not valid

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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?


    Floela Guest

  2. Similar Questions and Discussions

    1. System.InvalidCastException was unhandled
      For whatever reason, I am getting an invalidcast exception on this piece of code: gApp = CreateObject("AcroExch.App") This is just part of the...
    2. System.InvalidCastException in .Net client consuming Axis 1.1 java web service
      Hi, I am developing a .Net client for Axis 1.1 web service written in java. The wsdl is generated using, java2WSDL with these switches:: -y WRAPPED...
    3. dataGrid_ItemDataBound - System.InvalidCastException
      Essentially I am trying to convert a particular column in my datagrid to an hyperlink on the fly - I have pasted the relevant code and error below,...
    4. System.InvalidCastException: Specified cast is not valid. error when updating datagrid
      Hello, I am trying to update a record using a data grid. I Have a footer column in my data grid that allows users to add a new record. However, I...
    5. System.InvalidCastException: Specified cast is not valid.
      Hello, Does somebody haves a solution for this? Thanks! Line 74: if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==...
  3. #2

    Default Re: System.InvalidCastException: Specified cast is not valid

    Floela <floela@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 - <skeet@pobox.com>
    [url]http://www.pobox.com/~skeet/[/url]
    If replying to the group, please do not mail me too
    Jon Skeet Guest

  4. #3

    Default Re: 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);
    >
    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"];
    > }

    Christian Guest

  5. #4

    Default Re: System.InvalidCastException: Specified cast is not valid

    Floela <floela@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 - <skeet@pobox.com>
    [url]http://www.pobox.com/~skeet/[/url]
    If replying to the group, please do not mail me too
    Jon Skeet Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139