Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using Devexpress, When I choose a valid file to upload it says "Object reference not set to an instance of an object." If I put the filepath and into into the database manually and add the file to folder where web.config is set to I can see the file and download it. this is the part of code that breaks when trying

  protected void DocumentUpload_OnFileUploadComplete(object sender, FileUploadCompleteEventArgs e)
       {
           if (!e.UploadedFile.IsValid) return;

           GridViewDetailRowTemplateContainer container = ((Control)sender).NamingContainer as GridViewDetailRowTemplateContainer;
           Int32 index = container.VisibleIndex;

           var orderId = Convert.ToInt32(ViewOrdersGridView.GetRowValues(index, ViewOrdersGridView.KeyFieldName));
           var fileNumber = ViewOrdersGridView.GetRowValues(index, ElsaConstants.FileNumber).ToString();
           var userId = Convert.ToInt32(Session[ElsaConstants.UserId]);
           string directory = ConfigurationManager.AppSettings[ElsaConstants.UploadDirectory];
           if (!Directory.Exists(directory))
           {
               Directory.CreateDirectory(directory);
           }

           string fileType = Path.GetExtension(e.UploadedFile.FileName);
           string fileName = e.UploadedFile.FileName.Replace(fileType, string.Format("_{0}{1}", fileNumber, fileType));
           string resultFilePath = directory + fileName;
           e.UploadedFile.SaveAs(resultFilePath, true);//MapPath

           using (var context = new ELSAEntities())
           {
               var document = new Document
               {
                   OrderId = orderId,
                   DocumentPath = fileName,
                   DateCreated = DateTime.Now,
                   CreatedBy = userId
               };

               context.Document.Add(document);
               context.SaveChanges();
           }
       }<pre><pre><pre lang="c#">
Posted
Comments
DamithSL 18-Dec-14 12:38pm    
what is null? can you provide the full error stack trace?
Richard Deeming 18-Dec-14 12:44pm    
You have already posted this question in the C# forum:
http://www.codeproject.com/Messages/4966158/file-not-uploading-Help.aspx[^]

1 solution

Try setting key value of grid.
 
Share this answer
 

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