Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have created a fileupload and an imagebutton and also an repeater
<asp:Repeater ID="RepeaterDocument" runat="server">
                <ItemTemplate>
                    <asp:ImageButton ID="DoucumentBtn" runat="server"/>
                </ItemTemplate>
               
            
            </asp:Repeater>
 
when uploading a file in the fileupload i save it to the database like a attachment on a upload button that i have created, no my problem is that i dont now how to display the attacments to the repeater.
On my button were i upload to the sql and also want to display it in the repeaters /imagebutton
i have written following code
 
private void SetDocuments()
        {
 
            using (knowitCVdbEntities db = new knowitCVdbEntities())
            {
                try
                {
                    SPWeb web = SPContext.Current.Web;
 
                    SPWeb theSite = SPControl.GetContextWeb(Context);
                    SPUser theUser = theSite.CurrentUser;
                    string strUserName = theUser.LoginName;
 
                    //int documentId = Convert.ToInt32(HiddenFieldID.Value);

                    var theEmp = (from p
                                            in db.EMPLOYEES
                                        where p.username == strUserName
                                        select p).FirstOrDefault();
 
                    if (theEmp != null)
                    {
                        SPList myEmployeeDocList = web.Lists.TryGetList("Document cv");
 
                        SPListItem employeeDocListItem = myEmployeeDocList.Items.Add();
                        employeeDocListItem["Title"] = strUserName;
 
                        if (FileUploadDocuments.PostedFile != null && FileUploadDocuments.HasFile)
                        {
                            Stream fStream = FileUploadDocuments.PostedFile.InputStream;
                            byte[] empDocContents = new byte[fStream.Length];
                            fStream.Read(empDocContents, 0, (int) fStream.Length);
                            fStream.Close();
                            fStream.Dispose();
                            SPAttachmentCollection empDocAttachment =  employeeDocListItem.Attachments;
                            string empDocFileName = Path.GetFileName(FileUploadDocuments.FileName);
 
                            empDocAttachment.Add(empDocFileName, empDocContents);
                        }
 
                        employeeDocListItem.Update();
 
                        string empDocListUrl = web.Url + "/" + myEmployeeDocList.RootFolder.Url;
                        string empDocAttachmentUrl = empDocListUrl + "/attachments/" + employeeDocListItem.ID + "/" +
                                                     FileUploadDocuments.FileName;
                        
                        _emp = theEmp;
 
                        if (_emp != null)
                        {
                            var empDocument = new EMPLOYEE_DOCUMENT();
                            {
                                empDocument.document_name = empDocAttachmentUrl;
                                empDocument.employee_id = _emp.employee_id;
                                
                            };
  
                            _emp.EMPLOYEE_DOCUMENT.Add(empDocument);
                             db.SaveChanges();
                            
                        }
                      string filePath = empDocAttachmentUrl;
                      string extension = Path.GetExtension(filePath);
 
                      var imageDocumentDoc = (ImageButton)RepeaterDocument.FindControl("DoucumentBtn");
                      if (extension == ".doc" || extension == ".docx")
                      {
                          
                          imageDocumentDoc.ImageUrl = @"~/_layouts/images/Cv.Knowit/Ncrow-Mega-Pack-1-Word.bmp";
                          imageDocumentDoc.PostBackUrl = filePath;
 }
                      else if (extension == ".pdf")
                      { }
 
                      RepeaterDocument.DataSource = imageDocumentDoc;
                      RepeaterDocument.DataBind();
    
                    }
                }
                catch (Exception x)
                {
                    LabelErrorDocument.Text = x.Message;
                }
            }
        }
Posted 7-Apr-13 6:18am
Kurac1744
Edited 7-Apr-13 23:34pm
v2

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100