0
votes

Why can't I upload a .docx word document? I can upload other files like .mp3, .doc, and .txt.

The error reads:

System.Data.Entity.Validation.DbEntityValidationException was unhandled by user code HResult=-2146232032 Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Source=EntityFramework StackTrace: at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at Elearning_Assignment.Views.OnAssess.Student.OnAssess_MainPage_S.btnUpload_Click(Object sender, EventArgs e) in c:\Users\James\Desktop\WAD Assignment\Elearning_Assignment\Elearning_Assignment\Views\OnAssess\Student\OnAssess_MainPage_S.aspx.cs:line 53 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:

My code is as follows.

protected void btnUpload_Click(object sender, EventArgs e)
{
    int i = 0;

    HttpPostedFile file = FileUpload1.PostedFile;
    BinaryReader br = new BinaryReader(file.InputStream);
    byte[] buffer = br.ReadBytes(file.ContentLength);

    if (FileUpload1.HasFile)
    {
        string fileets = Path.GetExtension(FileUpload1.FileName);
        if (fileets.ToLower() != ".docx" && fileets.ToLower() != ".doc")
        {
            Response.Write("Only Allow .docx/.doc");
        }
        else
        {

            using (ELearningDatabaseEntities db = new ELearningDatabaseEntities())
            {
                db.OnlineAssessmentDocuments.Add(
                    new OnlineAssessmentDocument
                    {
                        DocID = "DOC00001",
                        DocName = file.FileName,
                        ContentType = file.ContentType,
                        DocSize = file.ContentLength,
                        DocContent = buffer,
                        LearnerID = "LEN00001"
                    }
                    );
                db.SaveChanges();
            }
        }
    }
}
1
Did you see EntityValidationErrors for more details, like the error message says? If not, go check it. What does it say? - mason
is that the error for the problem? - James0325

1 Answers

0
votes

You've got a validation error. Try this code (taken from this question) to see what the error is:

try
{
    db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
    foreach (var validationErrors in dbEx.EntityValidationErrors)
    {
        foreach (var validationError in validationErrors.ValidationErrors)
        {
            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
        }
    }
}

Once you know what the validation error is, correct it.