I was trying to add a product to Product Listings page. There were no attributes defined for it so SKU was to be base sku. When I clicked Save button on the SKU control, it gave me error Index out of range.
On digging into code I found that in save button's handler a call was made to CreteSKU [private void CreateSkus (AssociatedAttribute associatedAttribute, int level, string tempSku)]. Now as no attribute was defined for the product so when this method was called with this statement:
CreateSkus(associatedAttributeCollection[0], 0, product.BaseSku);
the associatedAttributeCollection[0] caused exception. So I wrapped call with check for count and nullreference of associatedAttributeCollection and modified CreateSKUs to return if this parameter is null. The modified code with line numbers is below.
/*File dashCommerce 3.0 Beta\Web\admin\controls\product\sku.ascx.cs*/
/// <summary>
/// Handles the Click event of the btnSave control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
protected void btnSave_Click (object sender, EventArgs e) {
try {
SkuCollection skuCollection = new SkuCollection();
Sku sku;
//if no attribute is defined then it throws an exception [dashtetanhaimein - 7/Mar/2K8]
if (associatedAttributeCollection == null || associatedAttributeCollection.Count < 1)
{
CreateSkus(null, 0, product.BaseSku);
}
else
{
CreateSkus(associatedAttributeCollection[0], 0, product.BaseSku);
}
for(int i = 0;i < skus.Count;i++) {
sku = new Sku();
sku.ProductId = productId;
sku.SkuX = skus
;
skuCollection.Add(sku);
}
skuCollection.SaveAll(WebUtility.GetUserName());
product.IsEnabled = true;
product.Save(WebUtility.GetUserName());
SkuCollection savedSkuCollection = LoadSkuCollection(productId);
if(savedSkuCollection.Count > 0) {
pnlSkuList.Visible = false;
pnlSkuInventory.Visible = true;
}
base.MasterPage.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblAttributesSaved"));
}
catch (Exception ex) {
Logger.Error(typeof(sku).Name + ".btnSave_Click", ex);
base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Creates the skus.
/// </summary>
/// <param name="associatedAttribute">The associated attribute.</param>
/// <param name="level">The level.</param>
/// <param name="tempSku">The temp sku.</param>
private void CreateSkus (AssociatedAttribute associatedAttribute, int level, string tempSku) {
//If there are no attributes it throws exception in caller - [dashtetanhaimein 7/Mar/2K8]
if (associatedAttribute == null)
return ;
int oldLevel = level;
string oldSku = tempSku;
for (int i = 0;i < associatedAttribute.AttributeItemCollection.Count;i++) {
tempSku += "-" + associatedAttribute.AttributeItemCollection
.SkuSuffix;
level += 1;
if (level < associatedAttributeCollection.Count) {
CreateSkus(associatedAttributeCollection[level], level, tempSku);
}
if (level == associatedAttributeCollection.Count) {
skus.Add(tempSku);
}
tempSku = oldSku;
level = oldLevel;
}
}