0

When running some code I've written on our production server or another developers machine it crashes on this line.

Ac_CalcDetail xy = db.Ac_CalcDetails.Where(chld => chld.CalcIdent.Equals(CalcIdent)).First();

CalcIdent is just a guid as a string.

I've ran sqlmetal on my local database where Ac_CalcDetail is our main table to create this strongly typed code and the class Ac_CalcDetail. The problem is when i run the code here on the production server or on one of the other developer's machines. We've been using LINQ for well over a year now and never had any problems so really confused about this. Previously the exceptions I've been getting have been very specific saying which field is missing which is brilliant, but from my research apparently this error could be a certain field having the wrong datatype. nightmare and completelely impossible to debug through just this error.

I think the only solution at present is to just back my database up in SQL Server Management Studio and then get everyone else to use this. As we're a small company we tend to just do whatever we want to our own databases, then create various SQL scripts to put on a dropbox folder to get our databases in sync (or at least what we thought was in sync). Not sure if this implies that we'll either have to stop using LINQ or start using some more advanced SQL stuff (SQL repositories or SQL database compare tools or etc).

It's very odd though as I know our databases haven't been completely the same for ages (if ever) so not sure why this error is happening now and why the error message doesn't say anything!

Daniel Gent
  • 91
  • 1
  • 6
  • What type is `Ac_CalcDetails` and what type is `CalcIdent` ? – SpaceBison Mar 07 '13 at 14:02
  • in the line of C# code above CalcIdent is a string . in the database it is a uniqueidentifier. I don't think this is the problem though as when other developers use my database it's fine. Ac_CalcDetails and Ac_CalcDetail are generated by sqlmetal (quite clever how it knows to remove the "s" from the table name for the row class). will step into the debugger to let you know exactly – Daniel Gent Mar 07 '13 at 14:10
  • Ac_CalcDetails is a table using generics and is of the type System.Data.Linq.Table This is all just automatically created by a script that uses sqlmetal. So on another developers machine it generates this error but it can be fixed by a) giving him a backup of my database, which he then restores, and uses this database instead b) have him run sqlmetal on his local database. but then when he checks in the new code I get the same error on mine – Daniel Gent Mar 07 '13 at 14:15
  • Have you been able to trace out the input/outputs when it works/doesnt work (outputs might be tricky when it doesn't work!) - sounds like something subtle changes based on whomever uses the sql tool last. – SpaceBison Mar 07 '13 at 14:22
  • Inlude the code leading up to this. I suspect your error is here .Equals(CalcIdent) in that it appears that is a class not a string so the cast is failing, but it's hard to tell from a single line of code. – Mike C. Mar 07 '13 at 17:52
  • @SpaceBison - I'm not sure what you mean here sorry. – Daniel Gent Mar 08 '13 at 11:54
  • Mike C - the line before is simply e2LinqDataContext db = new e2LinqDataContext(ConnectionStr); if you're not familiar with LINQ and sqlmetal then it won't mean very much. This is a problem with my database I'm running sqlmetal on not being the same as the production database the code is ran on. CalcIdent is set from the query string. say i don't set calcIdent to e.g. 'a797c493-1369-4dd6-865d-ecfd3c1bcac1' but instead 'frogspawn' then I get an error like this: "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)." – Daniel Gent Mar 08 '13 at 11:55
  • good idea though. sometimes it's the simplest things. i agree more code is better but the preceeding lines are full of initialising variables for this business object that aren't used much later. i could paste in all the sqlmetal generated LINQ but it would be meaningless there's so many fields! thanks for the help though guys – Daniel Gent Mar 08 '13 at 11:55

1 Answers1

0

Change

Ac_CalcDetail xy

to

var xy

than try debugging it. But clearly looks like you got some Types wrong.

Yahya
  • 3,386
  • 3
  • 22
  • 40
  • cheers Yahya, I think you're onto something there. to be honest I'm not sure why I haven't been using anonymous types all along. I'll step through my code again now using the same database that causes it to crash but use "var" in place of any classes like ac_calcDetails and see what happens. will post back cheers – Daniel Gent Mar 08 '13 at 11:59
  • I get exactly the same exception when running this code `var xyz = from cd in db.Ac_CalcDetails where cd.CalcIdent.Equals(CalcIdent) select cd;var xy = xyz.First();` the same exception happens on the `.First()` very wierd as when I inspect `xyz.First();` it is a class of type {e2Linq.Ac_CalcDetail} which looks like it should work to me – Daniel Gent Mar 08 '13 at 12:15
  • if it helps, even just calling `xyz.First();` in the debugger gives a `'xyz.First()' threw an exception of type 'System.InvalidCastException' e2Linq.Ac_CalcDetail {System.InvalidCastException}` – Daniel Gent Mar 08 '13 at 12:25
  • @Buswell Why dont you try something like this Ac_CalcDetail xy = db.Ac_CalcDetails.FristOrDefault(chld => chld.CalcIdent.Equals(CalcIdent)); – Yahya Mar 11 '13 at 09:32
  • Cheers Yahya, but with the line `Ac_CalcDetail xy = db.Ac_CalcDetails.FirstOrDefault(chld => chld.CalcIdent.Equals(CalcIdent));` it still throws the same exception _Specified cast is not valid._ – Daniel Gent Mar 11 '13 at 13:11
  • Few things to check here now. Ac_CalcDetails, is this a collection of Ac_CalcDetail? Also, try removing all "using" statements. And it will ask you to add reference. Its a good idea to do this and see if you are trying to reference same class or not! – Yahya Mar 11 '13 at 13:58