0

I am attempting to connect to an existing SQL Server database with a UWP Application and am using EF Core 2.2.4 but I am receiving the following error:

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)'

I have tried researching the problem on the internet using google and browsing forums, but so far nothing seems to point me toward a solution. I did discover an answer form '16 about SQLite being the only database option supported with EF & UWP but that was 3 years ago.

I am essentially just trying to get a list of a table object returned to display on my UWP in a ListView. The EF Core is in a separate project named "DataProject" with all the classes in a models folder that is added as a reference to my UWP project "UWPProject". Everything compiles ok, but when debugging I get the error 40 message when it tries to pull data from the SQL Database using EF Core.

UWPProject - MainPage.xaml File
<Page
    x:Class="UWPProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LocalPSInvestigation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:models="using:DataProject.Models"
    Loaded="Page_Loaded"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="My Complaints/Requests" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="MS Reference Sans Serif" FontSize="24" Width="300"/>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBox x:Name="idBox" Grid.Column="0" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="ID" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0" Width="320" />
            <TextBox x:Name="dateBox" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Date" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="addrBox" Grid.Column="2" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Address" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="countyBox" Grid.Column="3" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="County" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="cityBox" Grid.Column="4" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="City" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="phoneBox" Grid.Column="5" Grid.Row="0" TextWrapping="Wrap" Text="" PlaceholderText="Phone" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBox x:Name="searchBox" Grid.Column="4" Grid.Row="1" TextWrapping="Wrap" Text="" FontFamily="MS Reference Sans Serif" FontSize="12" TextAlignment="Center" VerticalAlignment="Center" BorderThickness="1" Grid.ColumnSpan="1" Margin="0,0,0,0" />
            <TextBlock x:Name="searchLabel" Grid.Column="3" Grid.Row="1" TextWrapping="Wrap" Text="Search:" VerticalAlignment="Center" HorizontalAlignment="Right" FontFamily="MS Reference Sans Serif" FontSize="12"/>
            <ListView Name="Complaints" SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.IsVerticalRailEnabled="True" ScrollViewer.VerticalScrollMode="Enabled"
                      ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.IsHorizontalRailEnabled="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" Grid.Row="2"
                      Grid.Column="0" Grid.ColumnSpan="6" Width="1920" Height="200">
                <ListView.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="ID" TextAlignment="Center" Width="320" />
                            <TextBlock Text="Date" TextAlignment="Center" Width="320" />
                            <TextBlock Text="Business Name" TextAlignment="Center" Width="200" />
                            <TextBlock Text="Address" TextAlignment="Center" Width="320" />
                            <TextBlock Text="County" TextAlignment="Center" Width="320" />
                            <TextBlock Text="City" TextAlignment="Center" Width="240" />
                            <TextBlock Text="Phone" TextAlignment="Center" Width="200" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.HeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="models:TableObject">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="ComplaintId" TextAlignment="Center" Text="{x:Bind ProgramComplaintId}" Width="320"/>
                            <TextBlock Name="Date" TextAlignment="Center" Text="{x:Bind Date}" Width="320"/>
                            <TextBlock Name="BusinessName" TextAlignment="Center" Text="{x:Bind BusinessName}" Width="200"/>
                            <TextBlock Name="Address" TextAlignment="Center" Text="{x:Bind Address}" Width="320"/>
                            <TextBlock Name="County" TextAlignment="Center" Text="{x:Bind County}" Width="320"/>
                            <TextBlock Name="City" TextAlignment="Center" Text="{x:Bind City}" Width="240"/>
                            <TextBlock Name="Phone" TextAlignment="Center" Text="{x:Bind Phone}" Width="200"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </StackPanel>
</Page>
UWPProject - MainPage.xaml.cs File
------------------------------------------------------------------------------
private void Page_Loaded(object sender, RoutedEventArgs e)
{
            using(var db = new dbContext())
            {
                ListViewName.ItemsSource = db.TableObject.ToList();
            }
}

------------------------------------------------------------------------------
DataProject - DBContext.cs File
------------------------------------------------------------------------------
public DbContext()
{
}

public DbContext(DbContextOptions<DbContext> options)
            : base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder.UseSqlServer("Server=XXXXXXX;Database=XXXXX;user id=XXXXXXXXX;password=XXXXXXX");
            }
}
------------------------------------------------------------------------------

I expected this to pull a list of the table objects and assign it to the items source, but instead I receive a connection error. I know the specified username/password works and that the server & database exists because I have used the scaffold command to generate the classes.

Does UWP support SQL Server connections with EF Core? I can't seem to find anything that states this is the case.

rientelfon
  • 33
  • 8
  • UWP does support SQL server connection with EF Core. Please follow this [thread](https://stackoverflow.com/a/50202912/6782612) to change connection string to use TCP/IP to see if it will work. – Xie Steven Jun 25 '19 at 06:59
  • I tried your suggestion, but am still getting the mentioned error. I have checked and connected to the SQL Server with the TCP/IP connection and credentials. I am definitely at a loss, I even activated Integrated Security as another user suggested. – rientelfon Jun 25 '19 at 11:56

1 Answers1

0

Thanks for the suggestion Xavier Xie, but it seems the issue was actually I didn't have the following capabilities enabled as shown in the picture below.

DatabaseIssue

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
rientelfon
  • 33
  • 8