Question

I've created a simple WPFApplication project that produce this error

This is the exact error:

Could not find file 'C:\Users\Andrei\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\dj2szrx3.5nm\e412xqna.uj1\Locality.xml'. E:\BugDemo2\BugDemo2\MainWindow.xaml 7 9 BugDemo2

MainWindow.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" x:Class="MainWindow"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <!--This is the line that Visual Studio says it produce the error -->
        <local:MainWindowViewModel x:Key="MainWindowViewModelDataSource" d:IsDataSource="True"/>
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource MainWindowViewModelDataSource}}"/>
</Window>

MainWindowViewModel.vb

Imports System.Data
Imports System.IO
Public Class MainWindowViewModel
    Shared ds As New DataSet("Locality")

    Public Sub New()
        MySub()
    End Sub

    Sub MySub()
        'this is the line that actually gives the error
        ds.ReadXml(
           Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location) &
           "\Locality.xml")
        MsgBox(ds.Tables.Count)
    End Sub

End Class

I have nothing in the code behind MainWindow.xaml.vb

Class MainWindow
End Class

The program does exactly what it is supposed to do, it loads Locality.xml to dataset correctly and shows how many tables are in it. If I move the ds variable in another module, or in another class, as a shared property I get

Object reference not set to an instance of an object

also as a Designtime only error.

I understand that Visual Studio doesn't know if Locality.xml, but why does it care and how do I make this error go away?

Was it helpful?

Solution

Use protective code, and more debuggable since you can check the variable before executing ReadXml:

Sub MySub()
    Dim path As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location) &
       "\Locality.xml"
    If IO.File.Exists(path) Then
      ds.ReadXml(path)
      MsgBox(ds.Tables.Count)
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top