2

I am massively in need to help. I have spent an entire day when I do not have even an hour to waste on a project. I have a Linq to SQL data layer and am doing a simple insert into the DB using the following code:

 using (var odc = new OrdersDataContext())
        {
            try
            {
                var id = GetNextOrderId(odc);
                order.ID = id;
                odc.tblOrders.InsertOnSubmit(order);
                odc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }

Due to other legacy code and this being a database that is running on SQL 2000 I cannot make database alterations, but this code was working fine at one point and then simply stopped working. It is saying that :

Cannot insert the value NULL into column 'ID', table 'Order'; column does not allow nulls. INSERT fails.

But as you can see from the code and what I can CLEARLY see as I step through the code, the ID column is getting a valid ID number. For some reason even though I am setting the ID field it is attempting to insert it as a null even though this code was working fine before. I've gone so far as to completely restore the database and rebuilt the data layer, but to no avail.

I'm so stumped on this and out of time that I'm beside myself.

webwires
  • 2,572
  • 3
  • 26
  • 44
  • 1
    There's absolutely no chance GetNextOrderId(odc) could return null without throwing an exception? – jpm Jun 27 '11 at 22:33
  • 1
    Have you tried running the profiler to see what SQL is being generated and sent to the DB? – kprobst Jun 27 '11 at 22:33
  • jpm ... I see the ID being properly set all the way until it goes into the InsertOnSubmit method but it keeps attempting to submit a null value. My assumption i sthat for some reason NONE of the values are being set, that the entire object goes through as null but I can't figure out why. At this point I have to start to assume that it is due to a cached version of code being used somewhere. Nothing else makes any sense. – webwires Jun 28 '11 at 00:46

1 Answers1

1

So, the answer to this is very strange and I'm not 100% certain of why. Essentially what I did was completely rebuild the LINQ to SQL data layer (merely dragging and dropping the same tables in the designer) and then completely cleaning and rebuilding the entire solution. After that the code magically worked again. No code change required.

For some reason the code was improperly cached and that caused it to not be able to transfer the object to the data layer (is my guess). It seems like a strange error to get back in that case.

webwires
  • 2,572
  • 3
  • 26
  • 44