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:
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.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