Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq DataContext doesn't recover after a caught exception?
    text
    copied!<p>I am using LINQ for the first time and I am running into some behavior I don't fully understand with the DataContext. Hopefully StackOverflow can help.</p> <p>Basically, it seems like once my DataContext encounters an exception, it either caches that exception or never fully recovers. In my specific case, once I try to pass a string that is too large to my DB, it throws the same exception for all following strings.</p> <p>I have a table as such:</p> <pre><code> DisplayString( StringID nvarchar(255) ,CultureCode varchar(25) ,DisplayString nvarchar(255) ) </code></pre> <p>In .Net I create a DataContext which I map to this table with a LINQ to SQL class. </p> <pre><code>Dim context As New LinqTableMappingDataContext( My.Settings.LocalizationStringsConnectionString) </code></pre> <p>I then create a bunch of test strings, iterate through each and send them to my table.</p> <pre><code>Dim arr As String() = {"aaaaa", "adsfs", "wefwf", "dfgsfg", "ergsdfg", "fsdgergd", "Sdgdfgegd", "ergdfgsfd"} For Each a As String In arr addToLinq(a) Next </code></pre> <p>This works fine. However if I add the following string at index 1, All subsequent inserts fail, throwing the same exception (SqlException) as the long string. </p> <pre><code>"WARNING: This computer program is protected by copyright law and international treaties. Unauthorized reproduction of this program, or any portion of it, may result in severe civil and criminal penalties, and will be prosecuted to the maximum extent possible under the law." </code></pre> <p>Here is my AddToLinq() method.</p> <pre><code>Private Sub AddToLinq(ByVal stringID As String) Dim f As New DisplayString Try Dim count As Integer = (From str In context.DisplayStrings _ Where str.StringID = stringID Select str).Count If count = 0 Then f.StringID = stringID f.CultureCode = "en-US" f.DisplayString = stringID context.DisplayStrings.InsertOnSubmit(f) context.SubmitChanges() Console.WriteLine("SUBMITTED: " &amp; stringID) End If Catch ex1 As SqlException Console.WriteLine("------------------------------------------------") Console.WriteLine("EXCEPTION: " &amp; ex1.Message) Console.WriteLine(stringID) Console.WriteLine(stringID.Length) Console.WriteLine("------------------------------------------------") Catch ex As DuplicateKeyException Console.WriteLine(ex.Message) End Try End Sub </code></pre> <p>If anyone could help me that would be great, thanks.</p>
 

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