0

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);
        }
    }

}

}

user1270384
  • 711
  • 7
  • 24
  • 53

3 Answers3

3

You're hiding your real exception stack trace by catching the exception and throwing it again like you're doing;

try {
    ...
}
catch(Exception ex) // catch all exceptions
{
    throw ex;       // throw the same exception, removing the old stack trace
}

Either you should just remove the whole try/catch, or change to;

try {
    ...
}
catch(Exception ex) // catch all exceptions
{
    throw;          // throw the last caught exception, keeping the stack trace
}

and you will suddenly be able to see the exact location of your original exception. I'm sure you will find your error right away.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
2

In my case, getting this error message, Oracle turned out to be returning an irrational number.

My code was throwing the "Specified cast is not valid" exception at adapter.Fill(ds);.NET was not interpreting the returned number correctly with the code I had been using. The addition of the line adapter.ReturnProviderSpecificTypes = true; Made it possible for .NET to interpret the number, rounded to the Nth decimal (last digit rounded up from 3 to 4 for some reason...): 2.25000000000000333333333333333333333334

    private static DataSet dataset_test(string sql)
    {
        DataSet ds = new DataSet();
        using (OracleConnection objConn = new OracleConnection(connectionstring))
        {
            OracleCommand objCmd = new OracleCommand();
            objCmd.Connection = objConn;
            objCmd.CommandText = sql;
            objCmd.CommandType = CommandType.Text;
            try
            {
                objConn.Open();
                OracleDataAdapter adapter = new OracleDataAdapter(objCmd);
                adapter.ReturnProviderSpecificTypes = true;
                adapter.Fill(ds);
            }
            catch (Exception)
            {
                throw;
            }
            objConn.Close();
            return ds;
        }
    }
Nikolaj
  • 21
  • 1
1

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.

Frater
  • 690
  • 3
  • 12
  • I think you just pointed out my error for me. I removed the throw exception just left throw and it gave the following in target site:{Int32 get_Int32()} I have yet to debug with breakpoints – user1270384 Apr 18 '12 at 21:02