Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm working with the following webpart: enter image description here

When i click on the save button i'm saving the information to a SharePoint list, and my picture i upload in the fileupload control to a picture library.

I want to show the image, and bellow that one instead of the name of the picture i wan't to show the title i'm saving to the List, in this case: ML Crankset.

The code i'm using to upload image is:

                    if(FileUpload_Pic != null)
                    {
                        if((FileUpload_Pic.PostedFile != null) && (FileUpload_Pic.PostedFile.ContentLength > 0))
                        {
                            Stream myStream = FileUpload_Pic.PostedFile.InputStream;
                            long iLength = myStream.Length;
                            imageData = new byte[(int) myStream.Length];
                            myStream.Close();

                            string fileName =
                                Path.GetFileName(FileUpload_Pic.PostedFile.FileName);

                            using (SPWeb myWeb = currentSite.OpenWeb())
                            {
                                SPPictureLibrary pic = (SPPictureLibrary) myWeb.Lists["Product pictures"];

                                SPFileCollection fileCol =
                                    ((SPPictureLibrary) myWeb.Lists["Product pictures"]).RootFolder.Files;
                                fileCol.Add(fileName, imageData);
                            }
                        }
                    }

And my whole Save button is:

    protected void Button_Save_Click(object sender, EventArgs e)
    {
        SPSite currentSite = SPContext.Current.Site;
        SPList myList = currentSite.RootWeb.Lists.TryGetList("Extern Products");

        try
        {
            byte[] imageData = null;

            if (myList != null)
            {
                if(TextBox_Name.Text == string.Empty &&
                    TextBox_ProdNum.Text == string.Empty &&
                    TextBox_MoreInfo.Text == string.Empty &&
                    TextBox_Color.Text == string.Empty &&
                    TextBox_ListPrice.Text == string.Empty)

                {
                    Label_Exception.Text = "Please enter a value.";
                }
                else
                {
                    SPListItem listItem = myList.Items.Add();

                    listItem["Title"] = TextBox_Name.Text;
                    listItem["ProductNumber"] = TextBox_ProdNum.Text;
                    listItem["Color"] = TextBox_Color.Text;
                    listItem["ListPrice"] = TextBox_ListPrice.Text;
                    listItem["MoreInformation"] = TextBox_MoreInfo.Text;

                    if(FileUpload_Pic != null)
                    {
                        if((FileUpload_Pic.PostedFile != null) && (FileUpload_Pic.PostedFile.ContentLength > 0))
                        {
                            Stream myStream = FileUpload_Pic.PostedFile.InputStream;
                            long iLength = myStream.Length;
                            imageData = new byte[(int) myStream.Length];
                            myStream.Close();

                            string fileName =
                                Path.GetFileName(FileUpload_Pic.PostedFile.FileName);

                            using (SPWeb myWeb = currentSite.OpenWeb())
                            {
                                SPPictureLibrary pic = (SPPictureLibrary) myWeb.Lists["Product pictures"];

                                SPFileCollection fileCol =
                                    ((SPPictureLibrary) myWeb.Lists["Product pictures"]).RootFolder.Files;
                                fileCol.Add(fileName, imageData);
                            }
                        }
                    }

                    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 = listItem.Attachments;
                        string fileName = Path.GetFileName(FileUpload_Doc.PostedFile.FileName);
                        attachments.Add(fileName, contents);

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

                    listItem.Update();

                    TextBox_Search.Text = string.Empty;
                    TextBox_Name.Text = string.Empty;
                    TextBox_MoreInfo.Text = string.Empty;
                    TextBox_ProdNum.Text = string.Empty;
                    TextBox_Color.Text = string.Empty;
                    TextBox_ListPrice.Text = string.Empty;

                    Label_Exception.Visible = false;

                    Dispose();   
                }
            }                   
        }
        catch (Exception x)
        {
            Label_Exception.Text = x.Message;
        }
     }

Any suggestions? Thanks. /Kristian

EDIT: Second question, is it possible to connect the image in picture library with the specific listitem in my SharePoint list? Because i'm working with a second webpart: enter image description here

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.