It is possible in Nopcommerce to create NopObjectContext object

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 anos atrás
Hello friend ,

   NopObjectContext db = new NopObjectContext();
                var Name = from c in db.ProductVariantAttributeValues
                          
                           where c.Quantity == Quantity1
                           select new
                           {
                               c.Name,
                               c.ProductVariantAttributeId
                           };

                ddlCardThickness.DataSource = Name;
              ddlCardThickness.DataTextField = "Name";
              ddlCardThickness.DataValueField = "ProductVariantAttributeId";
              ddlCardThickness.DataBind();


i was try to create database object to access a perticular value of a column , but i was not able to create the NopObjectContext object . i was face the following error

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.


Dipesh Jade (INDIA)
13 anos atrás
Use

var db = ObjectContextHelper.CurrentObjectContext
13 anos atrás
Also if the code is not running within the context of NopCommerce (i.e. it's a standalone web site or Windows app) you'll need to initialise the connection string in the business logic DLL. In NopCommerce this happens within global.asax Application_Start.
13 anos atrás
Below is some testcode I used to insert data into a table. Just remove the code after the "var context = " line and add your own linq.

Don't forget to add references to the nop dlls
This cs file is located in the root folder..nop is located in the root\Store folder


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Configuration;
using System.Configuration;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic;

namespace Root
{
    public partial class q1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NopConfig.ConnectionString = ConfigurationManager.ConnectionStrings["NopSqlConnection"].ConnectionString;
            var context = ObjectContextHelper.CurrentObjectContext;

            var addToCartWarnings = ShoppingCartManager.AddToCart(ShoppingCartTypeEnum.ShoppingCart, 70, "", 3.40M, 5);
            if (addToCartWarnings.Count == 0)
            {
                context.SaveChanges();
            }

        }

    }
}
13 anos atrás
That's good enough for testcode ajhvdb :)

If you're writing lots of functions I think it might be easy to forget to initialise the Nop DLL. That's why I've added a Nop connection string to our web.config and I call NopConfig.Init() from global.asax exactly as Nop does. I guess with a Windows app you'd do the same with app.config and Main() but I haven't tested it.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.