I am trying to add a record to my database table using LINQ to SQL and ASP.NET MVC 2.
The snippet in my controller that populates the LINQ object is this:
/* 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 */
The genesisRepository.CreateStream()
looks like this:
public partial class SqlGenesisRepository : IGenesisRepository
{
public long CreateStream(Stream stream)
{
streamTable.InsertOnSubmit(stream);
streamTable.Context.SubmitChanges();
return stream.StreamID;
}
}
When genesisRepository.CreateStream()
gets executed, I get this error:
Updated to more accurate error and stacktrace
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.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +127
When I put a breakpoint into the function, I see that stream
is not null. Some strings are null. Required strings are not null (IE: streamName = "name") and StreamID is 0
.
Where am I going wrong?
Edit: How `streamTable` is instanced
I don't think there is a problem with how I instance streamTable
but seeing as how I out of ideas and most people here think that it's null, here is the code that instances streamTable
.
public partial class SqlGenesisRepository : IGenesisRepository
{
private Table<Stream> streamTable;
public SqlGenesisRepository(string connectionString)
{
streamTable = (new DataContext(connectionString)).GetTable<Stream>();
}
public IQueryable<Stream> Streams { get { return streamTable; } }
}
And the SqlGenesisRepository is instanced in the controller class like this:
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 */
}