System.InvalidCastException: Specified cast is not valid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Floela

    #1

    System.InvalidCastException: 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?


  • Jon Skeet

    #2
    Re: System.InvalidC astException: Specified cast is not valid

    Floela <floela@benjami n.com> wrote:[color=blue]
    > 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);[/color]

    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.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Christian

      #3
      Re: System.InvalidC astException: Specified cast is not valid


      [color=blue]
      >
      > I get this when I try to pass a DataRow into an object method. The line[/color]
      that[color=blue]
      > causes this error is:
      >
      > policy.SetValue s(dr);
      >[/color]
      Inside SetValues there are many cast without checking,
      i wonder if they are all safe ???
      [color=blue]
      > 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"];
      > }[/color]


      Comment

      • Jon Skeet

        #4
        Re: System.InvalidC astException: Specified cast is not valid

        Floela <floela@benjami n.com> wrote:[color=blue][color=green]
        > > 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.[/color]
        >
        > 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.[/color]

        Yes there is - you've got lots of casts in SetValues. I'm sure it'll be
        one of those which is failing.
        [color=blue][color=green]
        > > Have you tried stepping through SetValues to see
        > > where the exception is thrown?[/color]
        >
        > No I haven't, as I'm actually coding the application using TextPad, not
        > VS.NET.[/color]

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

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...