G.Williams G.Williams - 4 months ago 25
Vb.net Question

(0xc0000005) 'Access violation': LINQ to SQL WPF VB.NET

Application compiles with no errors. Application runs but ends with no debug errors. Stepping through it, it cuts back to the designer at this line with Output showing:

"The program '[24680] AppName.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'."


Crashes on this line:

Dim c_InstanceDetail As t_Instance_Detail = (From InstDet In db.t_Instance_Details Where InstDet.ID = guInstance_ID Select InstDet).Single


In this sub:

Private Sub Refresh_PCA_InstanceDetail()
Dim c_InstanceDetail As t_Instance_Detail = (From InstDet In db.t_Instance_Details Where InstDet.ID = guInstance_ID Select InstDet).Single

Me.DataContext = c_InstanceDetail
Me.InstanceDetailTypeView = CType(CollectionViewSource.GetDefaultView(Me.DataContext), BindingListCollectionView)
End Sub


EDIT: Class Variables:

Private db As New AppDatabase_DBDataContext 'AppDatabase_DB being the name of the dbml file - AppDatabase_DB.dbml
Private InstanceDetailTypeView As BindingListCollectionView
Property guInstance_ID As Guid


t_Instance_Detail was created by LINQ to SQL classes and I've tried removing it and re-adding it - no change

This works (and only returns 1 record):

Dim c_InstanceDetail = (From InstDet In db.t_Instance_Details Where InstDet.ID = guInstance_ID Select InstDet)


I'm new to LINQ to SQL and WPF, so i'm assuming i'm doing something wrong or using the LINQ to SQL Classes in a why they are unintended. If someone can let me know what i'm doing wrong here, and how i should be doing this, that would be great thanks.

Answer

I think the issue was the possibility of not returning a record (even though it was returning 1 record at this point in time). The below fixed it:

Private Sub Refresh_PCA_Instance()
    Dim c_Instance As IEnumerable(Of t_Instance)
    c_Instance = (From Inst In db.t_Instances Where Inst.ID = guInstance_ID Select Inst)

    If c_Instance.Count = 1 Then
        Me.DataContext = c_Instance
        Me.InstanceDetailTypeView = CType(CollectionViewSource.GetDefaultView(Me.DataContext), BindingListCollectionView)
    ElseIf c_Instance.Count > 1 Then
        Me.DataContext = c_Instance.First
        Me.InstanceDetailTypeView = CType(CollectionViewSource.GetDefaultView(Me.DataContext), BindingListCollectionView)
    End If
End Sub

EDIT: The more i do this, i find better/other ways:

Private Sub Refresh_PCA_Instance()
    Dim c_Instance As IEnumerable(Of t_Instance)
    c_Instance = (From Inst In db.t_Instances Where Inst.ID = guInstance_ID Select Inst).First

    Me.DataContext = c_Instance
    Me.InstanceDetailTypeView = CType(CollectionViewSource.GetDefaultView(Me.DataContext), BindingListCollectionView)
End Sub

OR

Private Sub Refresh_PCA_Instance()
    Dim c_Instance As IEnumerable(Of t_Instance)
    c_Instance = (From Inst In db.t_Instances Where Inst.ID = guInstance_ID Select Inst)

    Me.DataContext = c_Instance.First
    Me.InstanceDetailTypeView = CType(CollectionViewSource.GetDefaultView(Me.DataContext), BindingListCollectionView)
End Sub