Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
            }

        }
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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!

share|improve this answer
    
hi yes thanks , but i get the problem then every time i press make changes is makes duplicates for me.. –  Martin Dec 6 '12 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 Dec 6 '12 at 11:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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