Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I'm working with the following webpart:
WebPart 1[^]

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:
C#
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:
C#
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;
        }
     }


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:
WebPart 2[^]

Any suggestions? Thanks. /Kristian
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900