I’m trying to create a new product but it give me a
“object not set to an instance of an object
at Dynamicweb.eCommerce.Products.Product.ExtendedSave(String VariantID)
at Dynamicweb.eCommerce.Products.Product.SaveAndConfirm(String ProductID, String ProductVariantID)
at Dynamicweb.eCommerce.Products.Product.Save()
at Eurolinea.EurolineaAdmin.import_click(Object sender, EventArgs e)”
when executing the save method from the Product object, here is the code.
protected void import_click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();
string csvLine;
string[] csvItems;
bool isUnknownGroup = true;
bool unknownExists = false;
Group unknownGroup = new Group();
client.Credentials = new System.Net.NetworkCredential(user.Text, password.Text);
int Stand = 0, Make = 1, Model = 2, Version = 3, Fuel = 4, Gearbox = 5, VehicleType = 6, Carrocaria = 7, Color = 8, Seats = 9, Year = 10, Quilometragem = 11, CubicCentimeters = 12, HorsePower = 13, FordDirect = 14, vat = 15, MakeCheckUps = 16, Warranty = 17, PreviousOwners = 18, Preco = 19, DescricaoEquipamento = 20, imagem1 = 21, imagem2 = 22, imagem3 = 23, imagem4 = 24, imagem5 = 25;
Stream csvStream = client.OpenRead(server.Text + "/" + file.Text);
StreamReader reader = new StreamReader(csvStream);
csvLine = reader.ReadLine();
while ((csvLine = reader.ReadLine()) != null)
{
DataTable dtProductID = new DataTable();
dtProductID.Load(Database.getDataReader("select max(productID) id from ecomproducts", ""));
string prdID = "PROD" + (Convert.ToInt32(dtProductID.Rows[0]["id"].ToString().Replace("PROD","")) + 1);
Product p = new Product(); //Product.Create(prdID);
p.ID = prdID;
csvItems = csvLine.Split(';');
/*SE NO CSV VIER O NOME DO GRUPO ENTÃO TEMOS DE FAZER DESTA FORMA*/
GroupCollection grupos = Dynamicweb.eCommerce.Products.Group.getGroups();
foreach (Group grp in grupos)
{
if (grp.Number.Equals("Unknown"))
{
unknownExists = true;
unknownGroup = grp;
}
if (grp.Number.Equals(csvItems[Stand]))
{
isUnknownGroup = false;
p.addGroup(grp);
}
}
if (isUnknownGroup)
{
if (unknownExists)
{
p.Groups.Add(unknownGroup);
}
else
{
Group newDestination = new Group();
newDestination.Name = "Unknown";
newDestination.Number = "Unknown";
newDestination.LanguageID = "LANG1";
string latestGroupId = Database.getDataSet("SELECT GroupID FROM EcomGroups ORDER BY GroupID DESC", String.Empty).Tables[0].Rows[0][0].ToString();
string newGroupId = "Group" + Convert.ToInt32(latestGroupId.Replace("GROUP", "")) + 1;
foreach (Dynamicweb.eCommerce.Shops.Shop shop in Dynamicweb.eCommerce.Shops.Shop.getShops(true))
{
if (shop.IsDefault)
{
newDestination.ShopID = shop.ID;
}
}
newDestination.Save(newGroupId);
}
}
string strTrue = Dynamicweb.Database.SqlBool(true);
string strFalse = Dynamicweb.Database.SqlBool(false);
p.ProductPriceString = csvItems[Preco];
p.LongDescription = csvItems[DescricaoEquipamento];
p.ProductFieldValues = new ProductFieldValueCollection();
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Make"), csvItems[Make]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Model"), csvItems[Model]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Version"), csvItems[Version]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Fuel"), csvItems[Fuel]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Gearbox"), csvItems[Gearbox]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("VehicleType"), csvItems[VehicleType]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Color"), csvItems[Color]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Seats"), csvItems[Seats]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Year"), csvItems[Year]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Quilometragem"), csvItems[Quilometragem]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("CubicCentimeters"), csvItems[CubicCentimeters]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("HorsePower"), csvItems[HorsePower]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("FordDirect"), csvItems[FordDirect].ToLower().Equals("n") ? strFalse : strTrue));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("vat"), csvItems[vat].ToLower().Equals("iva descriminado") ? strTrue : strFalse));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("MakeCheckUps"), csvItems[MakeCheckUps]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("Warranty"), csvItems[Warranty]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("PreviousOwners"), csvItems[PreviousOwners]));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("imagem1"), csvItems[imagem1] + ".jpg"));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("imagem2"), csvItems[imagem2] + ".jpg"));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("imagem3"), csvItems[imagem3] + ".jpg"));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("imagem4"), csvItems[imagem4] + ".jpg"));
p.ProductFieldValues.Add(new ProductFieldValue(new ProductField("imagem5"), csvItems[imagem5] + ".jpg"));
string[] nameSplited = csvItems[imagem1].Split('.');
string imageFormat = nameSplited.Last<string>();
p.Save();
}
}catch(Exception exc){
writeLog(exc.InnerException.Message, false);
writeLog(exc.StackTrace, false);
}