0

我的代码检查数据库中的更改,然后通过 Web 服务向客户端发送更新。

我让向客户端发送消息的部分工作正常,因为调用检查数据库中的更改。现在我已经添加了检查数据库更改的部分,我收到以下错误。

在调试我的代码时,错误指向代码最底部的异常,所以我不知道错误来自哪里,也无法修复它。

任何建议将不胜感激

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

}

}

4

3 回答 3

3

你通过捕获异常并像你正在做的那样再次抛出它来隐藏你真正的异常堆栈跟踪;

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

要么您应该删除整个 try/catch,要么更改为;

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

您将突然能够看到原始异常的确切位置。我相信你会马上发现你的错误。

于 2012-04-18T04:59:46.430 回答
2

就我而言,收到此错误消息后,Oracle 竟然返回了一个无理数。

我的代码在 .NET 中引发了“指定的强制转换无效”异常,adapter.Fill(ds);没有使用我一直使用的代码正确解释返回的数字。添加的adapter.ReturnProviderSpecificTypes = true;行使 .NET 可以解释数字,四舍五入到小数点后第 N 位(由于某种原因,最后一位从 3 向上舍入到 4...):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;
        }
    }
于 2014-06-04T12:18:38.330 回答
1

要尝试的一件事:在 try-catch 块的开始处设置断点,在 connStr = 行上,并逐行遍历代码,继续方法调用。通过这种方式,您可以快速跟踪代码的哪一部分引发了错误,然后回到该点以查找具体问题。(您还可以在重新抛出之前阅读原始异常和内部异常消息。)

鉴于您的问题的标题,因此假设您遇到的错误是强制转换异常,我首先要检查的是您的数据库数据检索代码。例如,您正在使用 sdr.GetInt32(0) ,如果无法将第 0 列转换为整数,则会引发此类错误。

希望这可以帮助。如果您能够找到更多信息,请将其发布,我们会看看我们是否可以提供更具体的建议。

于 2012-04-18T06:01:16.310 回答