Runtime errors when trying to connect to SQL Server Express

I

indtaz

I am a novice user of C# . I am trying to create a small application,
that connects to SQLServer and retrieves data into a List box.

However, I keep getting the following run-time error:

Cannot create instance of 'Window1' defined in assembly 'myTest_XAML,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has
been thrown by the target of an invocation. Error in markup file
'Window1.xaml' Line 1 Position 9.

When running under the debugger, this error occurs when executing the
statement :

using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(theTable);
}

I am unable to figure out exactly what is causing this error. Is it
because it is unable to connect to SQL Server ??
Any pointers/help would be greatly appreciated. I have included both
the XAML and C# code that I am using:


windows.xaml.cs
===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;

namespace MyTest_XAML
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{

DataTable theTable = new DataTable();

public Window1()
{
InitializeComponent();

String connString = @"Data Source=.\SQLEXPRESS;Initial
Catalog=AdventureWorks2000;Integrated
Security=True";

String query = @"SELECT
Product.ProductID,
Product.Name,
Product.ProductNumber,

FROM
Product";


using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(theTable);
}
DataContext = theTable;

}
}
}

Windows1.xaml
==============

<Window x:Class="MyTest_XAML.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="500" IsTabStop="True">

<Grid x:Name="MainGrid" Height="340" Width="423">
<Grid.Resources>
<Style x:Key="TitleText" TargetType="{x:Type TextBlock}" >
<Setter Property="FontFamily" Value="Segoe Black" />
<Setter Property="FontSize" Value="24px" />
<Setter Property="Foreground" Value="MidnightBlue" />
</Style>
<DataTemplate x:Key="ProductDataTemplate">
<StackPanel HorizontalAlignment="Center"
Background="{DynamicResource ListBoxGradient}"
Height="Auto" Width="150">
<Image Source="{Binding
Path=ThumbNailPhotoFilePath}"
ContextMenuService.IsEnabled="True"
ContextMenuService.ContextMenu="{Binding
Path=Name}"
HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>

</Grid.Resources>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="0.33*"/>
<ColumnDefinition Width="0.66*"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>

<TextBlock Style="{DynamicResource TitleText}">
<Span>Products List:</Span>
<Span FontStyle="Italic"> keeping you in the pack!</Span>
</TextBlock>

<ContentControl x:Name="MasterPane"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
RenderTransformOrigin="0.5,0.5">
<ListBox x:Name="MasterList"
Width="Auto" Height="280"
RenderTransformOrigin="0.5,0.5"
ItemsSource="{Binding Mode=Default}"
ItemTemplate="{DynamicResource
ProductDataTemplate}"
ItemContainerStyle="{DynamicResource
ProductTemplateItemStyle}"
HorizontalAlignment="Center"
SelectedIndex="0"
IsSynchronizedWithCurrentItem="True"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
</ContentControl>

<ContentControl x:Name="DetailsPane" Grid.Column="1" />

</Grid>
</Window>
 
M

Martin Honnen

indtaz said:
String connString = @"Data Source=.\SQLEXPRESS;Initial
Catalog=AdventureWorks2000;Integrated
Security=True";

Are there line breaks in the connection string or are they just
introduced by posting the code here? You might want to try a connection
string without line breaks.
 
I

indtaz

Are there line breaks in the connection string or are they just
introduced by posting the code here? You might want to try a connection
string without line breaks.

Hi Martin,

Thanks for your reply....

I tried without the line breaks but I am still getting the error.
 
W

Willy Denoyette [MVP]

indtaz said:
I am a novice user of C# . I am trying to create a small application,
that connects to SQLServer and retrieves data into a List box.

However, I keep getting the following run-time error:

Cannot create instance of 'Window1' defined in assembly 'myTest_XAML,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has
been thrown by the target of an invocation. Error in markup file
'Window1.xaml' Line 1 Position 9.

When running under the debugger, this error occurs when executing the
statement :

using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(theTable);
}

I am unable to figure out exactly what is causing this error. Is it
because it is unable to connect to SQL Server ??
Any pointers/help would be greatly appreciated. I have included both
the XAML and C# code that I am using:


windows.xaml.cs
===============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;

namespace MyTest_XAML
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{

DataTable theTable = new DataTable();

public Window1()
{
InitializeComponent();

String connString = @"Data Source=.\SQLEXPRESS;Initial
Catalog=AdventureWorks2000;Integrated
Security=True";

String query = @"SELECT
Product.ProductID,
Product.Name,
Product.ProductNumber,

FROM
Product";


using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter da = new SqlDataAdapter(query, conn);
da.Fill(theTable);
}
DataContext = theTable;

}
}
}

Windows1.xaml
==============

<Window x:Class="MyTest_XAML.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="400" Width="500" IsTabStop="True">

<Grid x:Name="MainGrid" Height="340" Width="423">
<Grid.Resources>
<Style x:Key="TitleText" TargetType="{x:Type TextBlock}" >
<Setter Property="FontFamily" Value="Segoe Black" />
<Setter Property="FontSize" Value="24px" />
<Setter Property="Foreground" Value="MidnightBlue" />
</Style>
<DataTemplate x:Key="ProductDataTemplate">
<StackPanel HorizontalAlignment="Center"
Background="{DynamicResource ListBoxGradient}"
Height="Auto" Width="150">
<Image Source="{Binding
Path=ThumbNailPhotoFilePath}"
ContextMenuService.IsEnabled="True"
ContextMenuService.ContextMenu="{Binding
Path=Name}"
HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>

</Grid.Resources>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="0.33*"/>
<ColumnDefinition Width="0.66*"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>

<TextBlock Style="{DynamicResource TitleText}">
<Span>Products List:</Span>
<Span FontStyle="Italic"> keeping you in the pack!</Span>
</TextBlock>

<ContentControl x:Name="MasterPane"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
RenderTransformOrigin="0.5,0.5">
<ListBox x:Name="MasterList"
Width="Auto" Height="280"
RenderTransformOrigin="0.5,0.5"
ItemsSource="{Binding Mode=Default}"
ItemTemplate="{DynamicResource
ProductDataTemplate}"
ItemContainerStyle="{DynamicResource
ProductTemplateItemStyle}"
HorizontalAlignment="Center"
SelectedIndex="0"
IsSynchronizedWithCurrentItem="True"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
</ContentControl>

<ContentControl x:Name="DetailsPane" Grid.Column="1" />

</Grid>
</Window>


Change this:
Product.ProductNumber,

FROM

into:
Product.ProductNumber FROM
...

And wrap your SqlDataAdapter da = new SqlDataAdapter(query, conn);
in a try block, and handle the exception thrown on you, it will tell you
exactly what the problem is with your code.

Also, as a novice user, you should start small, that is, start with a
console application, not with WPF, start learning the basics of the language
and the Framework class library, now you are throwing a lot of different
technologies (like XAML, WPF) in the mix and you sure will get lost.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top