tags:

views:

156

answers:

1

Hey guys, I'm having a weird time with Linq-To-SQL

I'm building a postit system that allows people to make postits and attach them to multiple objects. The database for this is a table that has the postits themselves (id, message, etc) and a linkstable which holds the records to which a postit is attached.

the weird thing I'm experiancing is the following. When I retrieve an object from the database (using Linq-To-SQL), do some data changing and submit it again, I experience no trouble whatsoever. Yet, when I try to make a new one I get an exception in the Submitchanges method in the datacontext: Specified Cast is not valid.

I've been looking on the web and mostly it involves some change in the mapping, but this shouldn't be the case as I can update without any problems.

T_PostIt np = new T_PostIt();
                np.CreatedOn = DateTime.Now;
                np.CreatedBy = Request.ServerVariables["REMOTE_USER"].ToString();
                np.MarkedForDeletion = false;
                np.Message = txtNewPostitMessage.Text;
                np.ModifiedBy = Request.ServerVariables["REMOTE_USER"].ToString();                    
                foreach (int i in ServerIds)
                {
                    T_PostIt_Link pil = new T_PostIt_Link();
                    pil.LinkType = 'S';
                    pil.LinkID = i;
                    pil.MarkedForDeletion = false;
                    np.T_PostIt_Links.Add(pil);        
                }
                dc.T_PostIts.InsertOnSubmit(np);
                dc.SubmitChanges();

I use the above code and can't seem to get what I'm doing wrong.

help anyone?

A: 

Have you tried updating the properties one by one, and then save the changes back to the database? It could be that updating the entity only fails when one specific value has changed. If I may guess, it could be that the value of CreatedOn cannot be cast to a valid DateTime in the database (due to culture settings). That would explain why updating goes OK - you're not changing the value of CreatedOn here. You do, however, when inserting a new entity.

Edit: maybe this is the problem you're facing. Also, be sure to read this thread, where the topic starter eventually points to the first thread mentioning that it is an apparant bug in Linq2Sql.

Razzie
I tried updating the createdon in the update method and it didn't crash ...
Jan W.
hmm, ok, my guess didn't turn out to be right then :-) I updated my answer with some links. You may face the same problem as some people do when using Linq2Sql. I hope it helps.
Razzie