Search code examples
c#exceptionwebservice-client.net

Specified cast is not valid issue


My code checks for changes in the database and then sends an update via a web service to the client.

I had the part sending the message to the client working fine because making the calls to check for changes in the database. Now that I have added the portion checking database changes I am getting the following error.

When Debugging my code the error points at the exception at the very bottom of the code so I have no idea where the error is coming from and no way of fixing it.

Any advise would be greatly appreciated

    SendInvUpdate.InvServices.UpdateRatePackagesRequest ur = new SendInvUpdate.InvServices.UpdateRatePackagesRequest();
    SendInvUpdate.InvServices.UpdateRatePackagesOperationResponse or = new SendInvUpdate.InvServices.UpdateRatePackagesOperationResponse();


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string connStr = ConfigurationManager.ConnectionStrings["bb"].ConnectionString;
            SqlConnection Con = new SqlConnection(connStr);
            Con.Open();
            SqlCommand cmd = new SqlCommand("invpush_PollForAvailableChanges", Con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter NewSysChangeVersionParam = new SqlParameter("@NewSysChangeVersion", SqlDbType.Int);
            NewSysChangeVersionParam.Value = (object)NewSysChangeVersionParam ?? DBNull.Value;
            NewSysChangeVersionParam.Direction = ParameterDirection.InputOutput;
            NewSysChangeVersionParam.SqlDbType = SqlDbType.BigInt;
            SqlDataReader sdr = cmd.ExecuteReader();

            InventoryPushSubscriptionRecord rec = new InventoryPushSubscriptionRecord();

            while (sdr.Read())
            {

                rec.InventoryPushSubId = sdr.GetInt32(0);
                rec.CMName = sdr.GetString(1);
                rec.NotifUrl = sdr.GetString(2);
                rec.Options = sdr.GetString(3);
                rec.LastSysChangeVersion = sdr.IsDBNull(4)?(long?)null:sdr.GetInt32(4);

            }

            if(!sdr.NextResult()) throw new System.Exception("Expected Result set 1 for InventoryChangeRecord");
            InventoryChangeRecord inrec = new InventoryChangeRecord();
            while (sdr.Read())
            {
                inrec.InventoryPushSubId= sdr.GetInt32(0);
                inrec.SysChangeVersion=sdr.IsDBNull(1)?(long?)null:sdr.GetInt32(1);
                inrec.InvDate=sdr.GetDateTime(2);
                inrec.ResId=sdr.GetInt32(3);
                inrec.RoomType=sdr.GetString(4);
                inrec.InvCount=sdr.GetInt32(5);
                inrec.ResName=sdr.GetString(6);

            }

            sdr.Close();
            sdr.Dispose();

            if (NewSysChangeVersionParam != null)
            {
                int ResId;
                Int64 ResoId;
                ResortId = inrec.ResId;
                SqlDataAdapter sda = new SqlDataAdapter("Select BID,RId from BBTest.bbtest.tblResMapping where BId=@ResId",Con);
                SqlParameter resId = new SqlParameter("@ResId", ResId);
                sda.SelectCommand.Parameters.Add(resId);
                DataSet ds = new DataSet();
                sda.Fill(ds, "tblresmapping");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ResoId = Convert.ToInt32(ds.Tables[0].Rows[0]["ResonlineId"]);
                }

                if (ds.Tables[0].Rows.Count != 0)
                {
                    Int64 RatePackageId;
                    SqlDataAdapter sqlda = new SqlDataAdapter("Select BId,BBRoom,ResRatePackageID from tblResRatePackages where BID =@ResortId", Con);
                    SqlParameter resI = new SqlParameter("@RId", resId);
                    sqlda.SelectCommand.Parameters.Add(resI);
                    DataSet dt = new DataSet();
                    sqlda.Fill(dt, "tblResRatePackages");
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        RatePackageId = Convert.ToInt64(dt.Tables[0].Rows[0]["ResRatePackageID"]);

                        Int64 HID = ResId;
                        Int64 HRID = RatePackageId;

                        SendInvUpdate.InvServices.UpdateRatePackagesRequest request = new SendInvUpdate.InvServices.UpdateRatePackagesRequest();


                        request.HotelId = account.HotelId;

                        int avail = inrec.InvCount;
                        DateTime frodte = inrec.InvDate;

                        int NoofRatePackages = 3;
                        UpdateRatePackageRequest[] RatePackages = new UpdateRatePackageRequest[NoofRatePackages];
                        string res;
                        request.RatePackages = new UpdateRatePackageRequest[NoofRatePackages];
                        request.RatePackages = RatePackages;

                        for (int i = 0; i < NoofRatePackages; i++)
                        {
                            UpdateRatePackageRequest rp = new UpdateRatePackageRequest();

                            request.RatePackages[i] = rp;

                            rp.RatePackageId = HRID;

                            rp.Rates = new RateDetails[NoofRatePackages];

                            for (int j = 0; j < NoofRatePackages; j++)
                            {

                                RateDetails rd = new RateDetails();
                                rp.Rates[j] = rd;

                                rd.Availability = avail;

                                rd.AvailabilityApplicationType = SendInvUpdate.InvServices.AvailabilityApplicationType.SET;

                                rd.FromDate = frodte;

                          //      rd.ToDate = todte;

                            }

                        }

                        SendInvUpdate.InvServices.InventoryServiceClient isc = new SendInvUpdate.InvServices.InventoryServiceClient();
                        or = isc.UpdateRatePackages(request);


                        res = or.Results.ToString();


                    }

                }
            }                             

        }

        catch (Exception ex)
        {
            throw (ex);
        }
    }

}

}


Solution

  • One thing to try: Set a breakpoint right at the start of the try-catch block, on the connStr = line, and step through the code line by line, progressing over method calls. In this way you can quickly track down which part of the code is throwing the error, and then home in on that point to look for the specific problem. (You will also be able to read the original exception and innerexception messages prior to the rethrow.)

    Given the title of your question, so the assumption that the error you are having is a casting exception, the first place I would check would be in your database data retrieval code. You are using sdr.GetInt32(0) for instance, this would throw that sort of error if column 0 couldn't be converted to an integer.

    Hope this helps. If you are able to track down more information, post it and we'll see if we can give more specific advice.