【问题标题】:Values not inserted in SQL Server databaseSQL Server 数据库中未插入的值
【发布时间】:2018-10-28 08:33:33
【问题描述】:

我有一个非常有趣的案例:在我的购物过程中的两个视图中,我有一个部分视图用于显示订单的总价格和交货价格。

此部分视图由ShowCartDiscountsPrices 操作呈现。最后,我必须使用实体框架在数据库MinDeliveryPriceMinDeliveryPriceDDSDeliveryPriceDeliveryPriceDDS 中插入。

奇怪的问题是,上周在我的 SQL Server 数据库中的两个案例中,我只有 DeliveryPrice 的值,而其他三列在我的 SQL Server 数据库中为空。

当我查看问题并使用相同的文章下订单时,例如一小时后,一切都很好,四列已填充,在此期间没有人对数据库进行任何更改。

我想知道可能是什么问题我可以编写一些脚本来记录是否在 SQL Server 或 IIS 中发生某种错误?

public ActionResult ShowCartDiscountsPrices(OrderDiscountPriceModel model, bool? ischange, int? deliverypreference)
{
    var cart = ShoppingCart.GetCart(WebsiteContext.CurrentUser != null ? (int?)WebsiteContext.CurrentUser.UserID : null);  

    decimal mindeliveryprice = 0;
    decimal mindeliverypricedds = 0;

    if (deliverypreference != null || Session["DeliveryPreference"] != null)
    {
        var parsedelprice = deliverypreference.HasValue ? deliverypreference.Value == 1 ? Decimal.TryParse(Settings.GetSettingByName("Eshop_SpeedyMinDeliveryPrice"), out mindeliveryprice) :Decimal.TryParse(Settings.GetSettingByName("Eshop_HomeMinDeliveryPrice"), out mindeliveryprice) : Decimal.TryParse(Session["MinDeliveryPrice"].ToString(), out mindeliveryprice );

        var parsedelpricedds = deliverypreference.HasValue ? deliverypreference.Value == 1 ? Decimal.TryParse(Settings.GetSettingByName("Eshop_SpeedyMinDeliveryPriceDDS"), out mindeliverypricedds) : Decimal.TryParse(Settings.GetSettingByName("Eshop_HomeMinDeliveryPriceDDS"), out mindeliverypricedds) : Decimal.TryParse(Session["MinDeliveryPriceDDS"].ToString(), out mindeliverypricedds );

        decimal deliveryprice;
        decimal deliverypricedds;

        ShoppingCartHelper.CalculateArticlesDeliveryPrice(mindeliveryprice, mindeliverypricedds, cart.ComputeTotalTransport(), cart.ComputeTotalTransportDDS(), out deliveryprice, out deliverypricedds);

        model.DeliveryPrice = deliveryprice;
        model.DeliveryPriceDDS = deliverypricedds;
        model.DeliveryPreference = deliverypreference.HasValue ? deliverypreference.Value : (int)Session["DeliveryPreference"];
        model.MinDeliveryPrice = mindeliveryprice;
        model.MinDeliveryPriceDDS = mindeliverypricedds;
        model.FinalSum += deliverypricedds;
    }
    else
    {
        if (isFromLastStep != null && bool.Parse(isFromLastStep.ToString()) == true)
            ViewBag.IncludeDelivery = true;
    }            

    if (deliverypreference != null)
    {
            Session["DeliveryPreference"] = deliverypreference;
            Session["MinDeliveryPrice"] = mindeliveryprice;
            Session["MinDeliveryPriceDDS"] = mindeliverypricedds;
    }

    ViewData.TemplateInfo.HtmlFieldPrefix = "OrderDiscoutPrice";
    return PartialView("_CartDiscountsPrices", model);
}

这是我的辅助函数,它们仅遍历包含文章的集合并计算总交付价格。

 public decimal ComputeTotalTransport()
        {
            decimal totalarticletransport = 0;                
            decimal articletransport;
            foreach (var article in lineCollection)
            {
                if (decimal.TryParse(article.Article.Transport.ToString(), out articletransport))
                {
                    totalarticletransport += article.Article.Quantity * articletransport;
                    articletransport = 0;
                }
            }
            return totalarticletransport;
        }
public decimal ComputeTotalTransportDDS()
        {
            decimal totaltransportdds = 0;           
            decimal articletransportdds;
            foreach (var article in lineCollection)
            {
                if (decimal.TryParse(article.Article.TransportDDS.ToString(), out articletransportdds))
                {
                    totaltransportdds += article.Article.Quantity * articletransportdds;
                    articletransportdds = 0;
                }
            }
            return totaltransportdds;
        }

【问题讨论】:

  • 好的,所以您的示例需要做一些工作。您显示了两个“计算...”函数,但这些函数似乎并未从您显示的操作代码中调用。您也不会在此处显示“模型”被实例化的任何代码,或者在任何将更改提交到您的数据库的地方。
  • 同意 - 如果可以,请显示 (a) 推送到数据库的模型和 (b) 实际插入数据库的代码。我想知道这是否与模型结构和 ORM 有关系

标签: sql-server asp.net-mvc


【解决方案1】:

我想建议您将 if 表达式进一步扩展一步,如下所示,以检查会话值是否大于0,如下所示。

if (Convert.ToString(deliverypreference) != "" && Convert.ToString(Session["DeliveryPreference"]) != "" && Convert.ToDecimal(deliverypreference) > 0 && Convert.ToDecimal(Session["DeliveryPreference"]) > 0)
{ 
    //Your insert logic as you have written.
}

您可能知道,有时您的会话变量可能不是空白或空字符串,但会话中的值等于 0。在这种情况下,您的代码将执行但数据不会正确插入。

第二种解决方法我想建议您通过在设计模式下修改表来禁用 SQL Server 列中的允许空值。

【讨论】:

    【解决方案2】:

    您好,我保证我会在答案中添加代码,但实际上问题出在重新启动 IIS 服务器上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多