Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does LINQ to SQL think think that my new object is null when I try to add it to the database?
    primarykey
    data
    text
    <p>I am trying to add a record to my database table using LINQ to SQL and ASP.NET MVC 2.</p> <p>The snippet in my controller that populates the LINQ object is this:</p> <pre><code>/* other code removed */ if (ModelState.IsValid) { var stream = new Genesis.Domain.Entities.Stream(); // Handle stream // Is this stream new? if (form.StreamID == 0) { // create new stream stream.StreamUrl = form.StreamUrl; stream.StreamName = form.StreamName; stream.StreamBody = form.StreamBody; stream.StreamTitle = form.StreamTitle; stream.StreamKeywords = form.StreamKeywords; stream.StreamDescription = form.StreamDescription; form.StreamID = genesisRepository.CreateStream(stream); // CreateStream() returns ID as long } /* other code removed */ </code></pre> <p>The <code>genesisRepository.CreateStream()</code> looks like this:</p> <pre><code>public partial class SqlGenesisRepository : IGenesisRepository { public long CreateStream(Stream stream) { streamTable.InsertOnSubmit(stream); streamTable.Context.SubmitChanges(); return stream.StreamID; } } </code></pre> <p>When <code>genesisRepository.CreateStream()</code> gets executed, I get this error:</p> <h3>Updated to more accurate error and stacktrace</h3> <pre><code> 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: Line 13: public long CreateStream(Stream stream) Line 14: { Line 15: streamTable.InsertOnSubmit(stream); Line 16: streamTable.Context.SubmitChanges(); Line 17: return stream.StreamID; Source File: C:\path\to\SqlGenesisRepositoryStreamPartial.cs Line: 15 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance) +18 System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance) +47 System.Data.Linq.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember) +106 System.Data.Linq.StandardTrackedObject.get_HasDeferredLoaders() +107 System.Data.Linq.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level) +175 System.Data.Linq.StandardChangeTracker.Track(Object obj, Boolean recurse) +83 System.Data.Linq.StandardChangeTracker.Track(Object obj) +12 System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity) +183 Genesis.Domain.Concrete.SqlGenesisRepository.CreateStream(Stream stream) in C:\Documents and Settings\bquakkelaar\Desktop\dropstuff\asp.net mvc\Genesis.0.02\Genesis.Domain\Concrete\SqlGenesisRepositoryStreamPartial.cs:15 Genesis_0_02.Controllers.AdminStreamController.StreamEdit(StreamEditModel form) in C:\Documents and Settings\bquakkelaar\Desktop\dropstuff\asp.net mvc\Genesis.0.02\Genesis.0.02\Controllers\AdminStreamController.cs:107 lambda_method(Closure , ControllerBase , Object[] ) +108 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +51 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +409 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +52 System.Web.Mvc.&lt;&gt;c__DisplayClassd.&lt;InvokeActionMethodWithFilters&gt;b__a() +127 </code></pre> <p>When I put a breakpoint into the function, I see that <code>stream</code> is not null. Some strings are null. Required strings are not null (IE: streamName = "name") and StreamID is <code>0</code>.</p> <p>Where am I going wrong?</p> <h2>Edit: How `streamTable` is instanced</h2> <p>I don't think there is a problem with how I instance <code>streamTable</code> but seeing as how I out of ideas and most people here think that it's null, here is the code that instances <code>streamTable</code>.</p> <pre><code>public partial class SqlGenesisRepository : IGenesisRepository { private Table&lt;Stream&gt; streamTable; public SqlGenesisRepository(string connectionString) { streamTable = (new DataContext(connectionString)).GetTable&lt;Stream&gt;(); } public IQueryable&lt;Stream&gt; Streams { get { return streamTable; } } } </code></pre> <p>And the SqlGenesisRepository is instanced in the controller class like this:</p> <pre><code>public class AdminStreamController : Controller { private IGenesisRepository genesisRepository; public AdminStreamController() { //genesisRepository = new FakeGenesisRepository(); genesisRepository = new SqlGenesisRepository(ConfigurationManager.ConnectionStrings["genesis"].ConnectionString); } /* rest of code removed for brevity */ } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload