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

i want to use an List that i create by me self that named MyList that done! Then on my webpart i want to use Fileupload to upload an picture to MyList, is it possible? Then for showing it on my page i want to use an Image control, Someone that know were to start?

My Code looks as following when saving to the list But here i am using Attachment

            SPSite site = new SPSite("http://wingtip") ;

            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists.TryGetList("MyList");




            try
            {
                if (list != null)


                {

                    SPListItem item = list.Items.Add();

                    if (TextBox_Name.Text==string.Empty&&TextBox_ProdNum.Text==string.Empty&&TextBox_ListPrice.Text==string.Empty&&TextBox_Color.Text==string.Empty&&TextBox_MoreInfo.Text==string.Empty)
                    {
                        Label1.Text = "Error:No Entered Value!´";
                        Label1.Visible = false;


                    }
                    else
                    {

                        item["Name"] = TextBox_Name.Text;

                        item["ProductNumber"] = TextBox_ProdNum.Text;

                        item["ListPrice"] = TextBox_ListPrice.Text;

                        item["Color"] = TextBox_Color.Text;

                        item["More Info"] = TextBox_MoreInfo.Text;


                        if (FileUpload_Pic.PostedFile != null && FileUpload_Pic.HasFile)
                        {
                            Stream fStream = FileUpload_Pic.PostedFile.InputStream;

                            byte[] contents = new byte[fStream.Length];
                            fStream.Read(contents, 0, (int)fStream.Length);
                            fStream.Close();
                            fStream.Dispose();

                            SPAttachmentCollection attachments = item.Attachments;
                            string fileName = Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
                            attachments.Add(fileName, contents);

                            //listItem["Attached FileName"] = fileName; // store the name of the file in a column for future requirements
                            item.Update();
                        }

                        if (FileUpload_Doc.PostedFile != null && FileUpload_Doc.HasFile)
                        {
                            Stream fStream = FileUpload_Doc.PostedFile.InputStream;

                            byte[] contents = new byte[fStream.Length];
                            fStream.Read(contents, 0, (int)fStream.Length);
                            fStream.Close();
                            fStream.Dispose();

                            SPAttachmentCollection attachments = item.Attachments;
                            string fileName = Path.GetFileName(FileUpload_Doc.PostedFile.FileName);
                            attachments.Add(fileName, contents);




                        }



                        item.Update();


                        //Rensar texboxarna


                        TextBox_Name.Text = string.Empty;

                        TextBox_ProdNum.Text = string.Empty;

                        TextBox_ListPrice.Text = string.Empty;

                        TextBox_Color.Text = string.Empty;

                        TextBox_MoreInfo.Text = string.Empty;

                        TextBox_Search.Text = string.Empty;


                        site.Dispose();

                    }

                    }



            }


    catch (Exception )
    {


        Label1.Text = "Error:No Entered Value!";
    }
share|improve this question
    
What is the problem with this code? Are you getting any errors? –  Arsalan Adam Khatri Dec 3 '12 at 14:14
    
Hi! No! But i would like to show my picture on an image control also on my webpart in my content... –  Martin Dec 3 '12 at 14:15

1 Answer 1

First you need to get the url for the list itself. The url for the list is simply:

string listUrl = web.Url + "/" + list.RootFolder.Url;

Then, you use the list url, the item id and the attachment name to construct the whole attachment url. The completed url is:

string attachmentUrl;
if (listItem.Attachments.Count > 0)
{
    string filename = listItem.Attachments[0];
    attachmentUrl = listUrl + "/attachments/" + item.ID + "/" + filename;
}

Once you have the URL, you can simply use this in Image Control:

Image1.ImageUrl = attachmentUrl;

Ref: Getting the Url for an SPListItem’s Attachment

share|improve this answer
    
hi Image1.ImageUrl = attachmentUrl; i wrote this in my selecteditem button from my listbox but attachmentUrl gets red –  Martin Dec 3 '12 at 15:09
    
under my textboxes that i have posted before , wrote , –  Martin Dec 3 '12 at 15:10
    
string listUrl = web.Url + "wingtip"; + list.RootFolder.Url; if (item.Attachments.Count > 0) { string filename = item.Attachments[0]; string attachmentUrl = listUrl + "/Attachments/" + item.ID + "wingtip"; + filename; } –  Martin Dec 3 '12 at 15:11
    
Updated the answer –  Arsalan Adam Khatri Dec 3 '12 at 16:20
    
it will help if u can answer my question –  Martin Dec 4 '12 at 9:03

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.