简体   繁体   中英

asp.net c# File uploading is working fine but error when I deployed in server

File uploading is working fine in Local but error when I deploy in server. The error message is

Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingConfigurationView.GetExceptionPolicyData(String policyName) +186 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.GetConfiguration(String id, IConfigurationSource configurationSource) +102 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) +94 Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.PreBuildUp(IBuilderContext context) +325 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +138

[BuildFailedException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, UIExceptionPolicy]) failed: Object reference not set to an instance of an object. (Strategy type ConfiguredObjectStrategy, index 2)] Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +498 Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing) +65 Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetimeContainer, String id, IConfigurationSource configurationSource) +729 Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.Create(String name) +187 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory) +90

[ExceptionHandlingException: The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, UIExceptionPolicy]) failed: Object reference not set to an instance of an object. (Strategy type ConfiguredObjectStrategy, index 2)] Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory) +494 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName, ExceptionPolicyFactory policyFactory) +75 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName) +255 MFBMS.Web.Focuspages.Videouploader.btnUploadDocument(Object sender, EventArgs e) in E:\\PROJECT\\DEVELOPMENT\\WEBSITE\\FOCUS-TUTORIAL\\Focustutorial\\MFBMS.Web\\Focuspages\\FileUploader.aspx.cs:185 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11594496 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +274 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1964

)

Here is the code that I try

protected void btnUploadDocument(object sender, EventArgs e)
    {
        try
        {
            string PathToSave = "";
            string FolderLocation = "";
            string FileName = "";
            string PathToDB = "";
            string FileType = "";
           
            if (!DocumentFileUpload.HasFile)
            {
                DocumentFileUpload.Focus();
                lblUploadMsg.Visible = true;
                lblUploadMsg.Text = "Select File Name.";
                lblUploadMsg.Focus();
                lblUploadMsg.CssClass = "FailedAlert";
                return;
            }
            else
            {
                FileUploaderBO objBO = new FileUploaderBO();
                FileUploaderData objdata = new FileUploaderData();

                //****** FOR SAVING IMAGE TO DATABASE******//
                //int length = DocumentFileUpload.PostedFile.ContentLength;
                //byte[] imgbyte = new byte[length];
                //HttpPostedFile img = DocumentFileUpload.PostedFile;
                //img.InputStream.Read(imgbyte, 0, length);
                //objdata.ByteImage = imgbyte;
                //****** End FOR SAVING IMAGE TO DATABASE******//

                FileType = ddlFileType.SelectedItem.Text;
                FolderLocation = Server.MapPath(@"~/FileFolder/" + FileType + "/");
                if (!Directory.Exists(FolderLocation))
                {
                    Directory.CreateDirectory(FolderLocation);
                }
                PathToSave = Server.MapPath(@"~/FileFolder/" + FileType + "/");

                FileName = DocumentFileUpload.FileName;
                PathToDB = "~/FileFolder/" + FileType + "/" + FileName;
                PathToDB = "~/FileFolder/" + FileType + "/" + FileName;

                string fileCheck = PathToSave + FileName;
                string tempFileToCheck = "";
                if (System.IO.File.Exists(fileCheck))
                {
                    int counter = 2;
                    while (System.IO.File.Exists(fileCheck))
                    {
                        tempFileToCheck = counter.ToString() + FileName;
                        fileCheck = PathToSave + tempFileToCheck;
                        counter++;
                        FileName = tempFileToCheck;
                        lblUploadMsg.Text = "A file with the same name is already exist." + "<br/> your file was saved as" + FileName;
                        lblUploadMsg.Focus();
                        lblUploadMsg.CssClass = "FailedAlert";
                    }
                }
                else
                {
                    PathToSave += FileName;
                    DocumentFileUpload.SaveAs(PathToSave);
                }

                objdata.FileTypeID = Convert.ToInt32(ddlFileType.SelectedValue == "" ? "0" : ddlFileType.SelectedValue);
                objdata.FileTypeName = ddlFileType.SelectedItem.ToString();
                objdata.FileName = FileName;
                objdata.FilePath = PathToDB;
                objdata.FileTitle = txtTitle.Text == "" ? null : txtTitle.Text;
                objdata.FileDecription = txtDecription.Text == "" ? null : txtDecription.Text;

                List<FileUploaderData> DataList = new List<FileUploaderData>();
                DataList = objBO.UpdateUploadFile(objdata);
                if (DataList.Count > 0)
                {
                    if (DataList[0].SQLOutput == 1)
                    {
                        lblUploadMsg.Visible = true;
                        lblUploadMsg.Text = "Uploaded Successfully.";
                        lblUploadMsg.Focus();
                        lblUploadMsg.CssClass = "SuccessAlert";
                    }
                    else if (DataList[0].SQLOutput == 2)
                    {
                        lblUploadMsg.Visible = true;
                        lblUploadMsg.Text = "Uploaded Update Successfully.";
                        lblUploadMsg.Focus();
                        lblUploadMsg.CssClass = "SuccessAlert";
                    }
                }
                else
                {
                    lblUploadMsg.Focus();
                    lblUploadMsg.Visible = true;
                    lblUploadMsg.Text = "Not Uploaded due to system error.";
                    lblUploadMsg.CssClass = "FailedAlert";
                }
            }
        }
        catch (Exception ex)
        {
            HandlePolicyException.ExceptionHandler(HandlePolicyException.PolicyName.UIExceptionPolicy, ex, "1000001");
            LogManager.LogError(ex, EnumErrorSource.Web);
            MessageAlert_.ShowMessage(ex, "system", 0);
        }
    }

I have sometimes seen this error. In one of the cases, it was that the connection to the db wasn't succeeding. Like the comments above suggested, try checking whether the password for prod is correct. You could probably try connecting to the db immediately at the top of your method and see if the code breaks on that line when you debug it.

I would also try looking in the decompiled code for this if you have access to it to see if there are any clues there.

You just have to check line 185 in FileUploader.aspx.cs. Null reference exceptions are thrown when there's an attempt to access a member of an object set as null. Once you get the affected line you need to check what are all the objects that might be null and try to understand why they might be null. Troubleshooting this kind of issues sometimes is easy but sometimes require you to add additional logging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM