0

hi i am using built in attachment in a list i have created an textbox and a button and i would like to show the url of the attachment in the textbox TextBox1.Text = customer["Attachments"].ToString();

SPWeb web = SPContext.Current.Web;

        SPList customerList = web.Lists.TryGetList("MyList");
        if (customerList != null)
        {
            SPQuery query = new SPQuery();
            query.Query =
                string.Format(
                    "<Where><BeginsWith><FieldRef Name='ProductNumber' /><Value Type='Text'>{0}</Value></BeginsWith></Where>",
                    prod);
            query.RowLimit = 10;

            SPListItemCollection items = customerList.GetItems(query);

when writing like this i get in the textbox true... whats the problem???enter image description here

this solved my problem...

 SPAttachmentCollection collAttachments = customer.Attachments;

                if (collAttachments.Count > 0)
                {
                    ListBox1.Items.Clear();
                    for (int i = 0; i < collAttachments.Count; i++)
                    {
                        ListBox1.Items.Add(collAttachments[i]);
                    }
                }

                customer.Update();
            }

        }

1 Answer 1

1

If I am not mistaken, customer["Attachments"] will return for you an SPAttachmentCollection, so converting that to string is probably not what you want.

Please try something like this to obtain the URL of each attached file from the attachment collection of a list item.

SPAttachmentCollection collAttachments = oListItem.Attachments;

for (int i=0; i<collAttachments.Count; i++)
{
   TextBox1.Text += collAttachments[i];
}

I hope this is what you were looking for :)

Cheers!

2
  • hi yes thanks , but i get the problem then every time i press make changes is makes duplicates for me..
    – Batistuta
    Commented Dec 6, 2012 at 11:25
  • Ok, the one above was just a sample. If you want it translated to your particular case, I think it should be something like this: get rid of the for cycle. Just use: TextBox1.Text = collAttachments[0]; Of course, checking first if (collAttachments.Count > 0) wouldn't hurt :)
    – Norbert
    Commented Dec 6, 2012 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.