Nanaki
Nanaki

Reputation: 197

Error when filling DataGrid with SQL table

I am trying to populate a WPF DataGrid with a SQL Server table, but I am getting this exception when I try to start the App (and it enters Break Mode):

An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll

Additional information: The type initializer for 'System.Windows.Application' threw an exception.

MY CODE

App.config

<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>

<connectionstrings>
  <add connectionstring="Data Source=*.*.*.*; User Id=**;Password=*****; Initial Catalog=Visitas;" name="ConString"/>
</connectionstrings> 

MainWindow

public partial class MainWindow : Window
{        
    public MainWindow()
    {
        InitializeComponent();            
    }

    private void Fill()
    {
        string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        string CmdString = string.Empty;
        using (SqlConnection con = new SqlConnection(ConString))
        {
            CmdString = "SELECT * FROM employees_ext";
            SqlCommand cmd = new SqlCommand(CmdString, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("EMPLOYEES_EXT");
            sda.Fill(dt);
            dataGridE.ItemsSource = dt.DefaultView;
        }
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Fill();
    }
}

Pretty simple code, but already checked everything and read tons of google info, but I am still not able to solve it :\

Upvotes: 0

Views: 69

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39966

Since the XML is case-sensitive your connectionstring spelling is incorrect. So you should note that attribute names are case-sensitive. The s in connectionString should be Uppercase:

<connectionStrings>
  <add connectionString="Data Source=*.*.*.*; User Id=**;Password=*****; Initial Catalog=Visitas;" name="ConString"/>
</connectionStrings> 

Upvotes: 2

Related Questions