views:

92

answers:

3

I got some help on here last night about getting an the index of a collection. Here is the code I'm using.

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            for (int i = 1; i < _prod.ActiveProductItemCollection.Count; i++)
            {

                sb.Append("<script type='text/javascript'>");
                sb.Append("mboxCreate(\"product_productpage_rec{0}\")", i);
                sb.Append("\"entity.id=" + _prodID + "\",");
                sb.Append("\"entity.categoryId=" + _categoryID + "\",");
                sb.Append("\"entity.name=" + _prod.ActiveProductItemCollection[i].Title + "\",");
                sb.Append("\"entity.pageURL=" + Request.Url.ToString() + "\",");
                //The following value has been taken from the productImageControl code behind.
                //Might have to refactor in future as a property of the image control.
                string filename = AppSettingsManager.Current.ProductImagePathLarge + _prod.ActiveProductItemCollection[i].Sku
                    + AppSettingsManager.Current.ProductImageExtension;
                sb.Append("\"entity.thumbnailURL=" + filename + "\",");
                sb.Append("\"entity.inventory=" + _prod.ActiveProductItemCollection.Count + "\",");
                sb.Append("\"entity.value=" + _prod.ActiveProductItemCollection[i].ActualPrice + "\",");
                sb.Append("\"entity.ProductItemID=" + _prod.ActiveProductItemCollection[i].Sku + "\",");
                sb.Append("\"entity.addToCartImg=~/Images/Buttons/btn_AddToCartFlat.gif\");<");
                //The last line has to be /script. < inserted on prev line. do not change it or bad things will happen.            
                sb.Append("/script>");
            }
            this.LiteralMBoxScript.Text = sb.ToString();

What I want to do is get the index of the item in the collection after productpage_rec.

So for instance, the first product would generate like

mboxCreate("product_productpage_rec1"

and so on and so forth for each product in the collection. When I try to build I get the follwong error:

"cannot convert from string to char"

Its tripping up on the product_productpage_rec{0}

What am I missing?

Thanks in advance.

+6  A: 
  sb.AppendFormat("mboxCreate(\"product_productpage_rec{0}\")", i);

should work

Steve Townsend
ahh thank you! it did. I'll mark as answered as soon as it lets me. Thanks again, knew it was something small like that.
tking
@tking - great, thanks
Steve Townsend
+3  A: 

Change this

sb.Append("mboxCreate(\"product_productpage_rec{0}\")", i);

To this:

sb.AppendFormat("mboxCreate(\"product_productpage_rec{0}\")", i);
Winston Smith
A: 

You need to use

sb.Append(String.Format("mboxCreate(\"product_productpage_rec{0}\")", i));
Thomas James
Never mind ... beaten to it with better answers.
Thomas James
Your answer looked familiar! lol
MattC
Hmm why the downvote? Its not the best answer but its not blatantly wrong.
Thomas James