I'm encountering an issue while attempting to get this code working smoothly, as shown in a C# WPF tutorial for creating a registration menu (which is now three years old; hopefully, it's still relevant). I'm struggling with handling an exception and would appreciate some guidance.
The issue revolves around a detailed exception, "System.Windows.Markup.XamlParseException," causing the program to halt in XamlReader.cs with an error as follows:
Here's the full exception stack: `System.Windows.Markup.XamlParseException HResult=0x80131501 Message="Calling a constructor for type 'UsersApp.MainWindow' that satisfies the specified binding constraints resulted in an exception being thrown.': line number '8' and line position '9'.. Source=PresentationFramework StackTrace: at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) in System.Windows.Markup\XamlReader.cs:line 405
This exception was originally thrown at this call stack: System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(bool) System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors) System.Configuration.ClientConfigurationSystem.EnsureInit(string)
Inner Exception 1: TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
Inner Exception 2: ConfigurationErrorsException: Configuration system failed to initialize
Inner Exception 3: ConfigurationErrorsException: Unrecognized configuration section system.data. (C:\Users\nikit\OneDrive\Рабочий стол\University\University_KHPI_secondyear_2-1\C#_General\C#_Youtube_Course_Projects\C#_WPF\WPF-in-VisualStudio2022\UsersApp\bin\Debug\net7.0-windows\UsersApp.dll.config line 26)
`
Additionally, here are sections of the project that might be causing this exception:
MainWindow.xaml:
<Window x:Class="UsersApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UsersApp"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Title="Application for users" Height="600" Width="800">
<Grid Background="#ffdadada">
<Border MinWidth="400" Margin="15" Background="white" VerticalAlignment="Center" Padding="30" MaxWidth="550" CornerRadius="20">
<Border.Effect>
<DropShadowEffect BlurRadius="30" Color="LightGray" ShadowDepth="0" />
</Border.Effect>
<StackPanel>
<TextBlock Text="Hello you!" FontSize="30" FontWeight="Bold" Margin="0 0 0 20" />
<TextBlock x:Name="exampleText" FontWeight="Bold" Margin="0 0 0 20" />
<Grid Margin = "0 0 0 20">
<Button HorizontalAlignment="Left" Content="Registration" />
<Button HorizontalAlignment="Right" Content="Log in" Style="{StaticResource MaterialDesignFlatButton}" />
</Grid>
<TextBox x:Name="textBoxLogin" materialDesign:HintAssist.Hint="Enter login" Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
<PasswordBox x:Name="passBox" materialDesign:HintAssist.Hint="Enter password" Style="{StaticResource MaterialDesignFloatingHintPasswordBox}"/>
<PasswordBox x:Name="passBox_2" materialDesign:HintAssist.Hint="Repeat password" Style="{StaticResource MaterialDesignFloatingHintPasswordBox}" />
<TextBox x:Name="textBoxEmail" materialDesign:HintAssist.Hint="Email" Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
<Button Content="Register now!" Margin="0 20 " Click="Button_Rag_Click" />
</StackPanel>
</Border>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UsersApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ApplicationContext db = new ApplicationContext();
public MainWindow()
{
InitializeComponent();
List<User> users = db.Users.ToList();
string str = "";
foreach (User user in users)
{
str += "Login: " + user.Login + " | ";
}
exampleText.Text = str;
}
private void Button_Rag_Click(object sender, RoutedEventArgs e)
{
string login = textBoxLogin.Text.Trim(); //deleting spaces
string pass = passBox.Password.Trim();
string pass_2 = passBox_2.Password.Trim();
string email = textBoxEmail.Text.Trim().ToLower();
if (login.Length < 5)
{
textBoxLogin.ToolTip = "This line is incorrect!";
textBoxLogin.Background = Brushes.DarkRed;
} else if (pass.Length < 5)
{
passBox.ToolTip = "This line is incorrect!";
passBox.Background = Brushes.DarkRed;
} else if (pass != pass_2)
{
passBox_2.ToolTip = "This line is incorrect!";
passBox_2.Background = Brushes.DarkRed;
} else if (email.Length < 5 || !email.Contains("@") || !email.Contains("."))
{
textBoxEmail.ToolTip = "This line is incorrect!";
textBoxEmail.Background = Brushes.DarkRed;
} else
{
textBoxLogin.ToolTip = "";
textBoxLogin.Background = Brushes.Transparent;
passBox.ToolTip = "";
passBox.Background = Brushes.Transparent;
passBox_2.ToolTip = "";
passBox_2.Background = Brushes.Transparent;
textBoxEmail.ToolTip = "";
textBoxEmail.Background = Brushes.Transparent;
MessageBox.Show("Excellent!");
User user = new User(login, email, pass);
db.Users.Add(user);
db.SaveChanges();
}
}
}
}
User.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UsersApp
{
internal class User
{
public int Id { get; set; }
private string login, email, pass;
public string Login
{
get { return login; }
set { login = value; }
}
public string Email
{
get { return email; }
set { email = value; }
}
public string Pass
{
get { return pass; }
set { pass = value; }
}
public User() { }
public User(string login, string email, string pass)
{
this.login = login;
this.email = email;
this.pass = pass;
}
}
}
App.xaml:
<Application x:Class="UsersApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UsersApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
UsersApp.dll.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<!-- Если не работает, то сделайте следующее. Замените все содержимое файла, но объект startup установите такой же, как был у вас ранее -->
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\itproger.db" providerName="System.Data.SQLite" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>
ApplicationContext.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace UsersApp
{
// С помощью этого класса мы сможем
// подключаться с необходимой базой данных
internal class ApplicationContext : DbContext
{
public DbSet<User> Users { get; set; }
public ApplicationContext() : base("DefaultConnection") { }
}
}
For instance, here is a screenshot of SolutionExplorer:
I've attempted to wrap suspicious methods within try/catch structures, particularly related to the database variable. Additionally, I've double-checked the correctness of the paths in the configuration file for the database and the "<system.data>" tags.
My expectation was to resolve the XamlParse Exception, allowing the program to register users and send data to the SQLite database.