0

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();
            }
        }
    }
}
James0325
  • 51
  • 1
  • 9

1 Answers1

0

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.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • Thanks for your help, i had solved the problem because my database needed to change to varChar(200), original was varChar(50) :) – James0325 Jul 31 '14 at 04:25
  • @James0325 Glad you solved it. Remember you could have probably figured this one out yourself by reading the error message and following the directions. If it still wasn't clear from the error message, then throw it into your favorite search engine and see what comes up. – mason Jul 31 '14 at 13:38