Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Values not inserted in SQL Server database

I have a really interesting case: in two views during my shopping process I have a partial view for displaying the total price of the order and delivery prices.

This partial view is rendered by the ShowCartDiscountsPrices action. At the end I have to insert using Entity Framework in the database MinDeliveryPrice, MinDeliveryPriceDDS, DeliveryPrice, DeliveryPriceDDS.

The strange problem is that in two cases last week in my SQL Server database, I have value only for DeliveryPrice AND SOMEHOW THE OTHER THREE COLUMNS ARE null in my SQL Server database.

And when I look at the problem and make an order with the same articles for example an hour after that everything is fine and the four columns are populated and during this period of time nobody has made any changes in the database.

I am wondering what can be the problem can I write some script for logging to see if some kind of mistakes occurs either in SQL Server or IIS maybe?

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

And here are my helper functions that only iterate through a collection with articles and calculate the Total Delivery Prices.

 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;
        }
like image 794
Tania Marinova Avatar asked Oct 28 '18 08:10

Tania Marinova


1 Answers

I want to suggest you to extend your if expression one step further as shown below to check whether the session value is greater than 0 as shown below.

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

As you might be aware sometime when your session variable might not be blank or null string but the value in session is equal to the 0. In that case your code will execute but the data will not inserted properly.

The second workaround I want to suggest you to disable allow null value in SQL Server column by modifying table in design mode.

like image 101
Suraj Kumar Avatar answered Oct 01 '22 04:10

Suraj Kumar