Thursday 19 October 2017

c# - NullReferenceException when saving to database with asp.net using EF





I have a movie database where I'm trying to save a genre. I have the
same project in winforms and there the code works, so it's probably something simple
that I missed.



Anyways, here is the
code:



 MovieCollectionEntities db
= new MovieCollectionEntities();
Genre g = new Genre();

g.GenreName = TextBoxGenresAdd.Text;
db.Genres.Add(g);

db.SaveChanges();


The
error message I get
is:



System.NullReferenceException
was unhandled by user code
HResult=-2147467261
Message=Object
reference not set to an instance of an
object.


edit: />Worth mentioning is probably that I'm using master paging and the code is from the
AddGenre.aspx.



edit2: Complete answer can be
found @ href="https://stackoverflow.com/questions/17136455/nullreferenceexception-in-dbcontext-savechanges">NullReferenceException
in DbContext.saveChanges()


class="post-text" itemprop="text">
class="normal">Answer



@Abhinav
linked to the correct page. The problem was that the aspx pages had the same names as
the entity names which made the program crash.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...