0

I'm wasting whole sunday to make simple filling to datagrid in C# WPF app. All in all i make it like all web examples but the code (below) throws NullReferenceException.

SqlConnection connection = new SqlConnection("Data Source=DOM\\SQL2012EVAL;Initial Catalog=PanelOK;Integrated Security=True");
SqlCommand cmdSel = new SqlCommand(outputInfo, connection);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmdSel);
da.Fill(dt);
klientDataGrid.DataContext = dt;

Weird thing also is that compiler go through the code in debug mode even though it's in event triggered by textchange property of textbox.

The Xaml declaration is as follows:

<Window x:Class="Panel_OK.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="768" Width="1024" xmlns:my="clr-namespace:Panel_OK">
    <Window.Resources>
        <my:PanelOKDataSet x:Key="panelOKDataSet" />
        <CollectionViewSource x:Key="klientViewSource" Source="{Binding Path=Klient, Source={StaticResource panelOKDataSet}}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource klientViewSource}">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="25,24,0,0" Name="SearchClientTB" VerticalAlignment="Top" Width="306" Text="Szukaj klienta ..." TextChanged="SearchClientEvent" />
        <DataGrid EnableRowVirtualization="True" Height="114" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="25,53,0,0" Name="klientDataGrid" RowDetailsVisibilityMode="Visible" VerticalAlignment="Top" Width="951" DataContext="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="iDColumn" Binding="{Binding Path=ID}" Header="ID" IsReadOnly="True" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="nazwa_KODColumn" Binding="{Binding Path=Nazwa_KOD}" Header="Nazwa KOD" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="miastoColumn" Binding="{Binding Path=Miasto}" Header="Miasto" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="imieColumn" Binding="{Binding Path=Imie}" Header="Imie" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="nazwiskoColumn" Binding="{Binding Path=Nazwisko}" Header="Nazwisko" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="telefonColumn" Binding="{Binding Path=Telefon}" Header="Telefon" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="eMailColumn" Binding="{Binding Path=eMail}" Header="e Mail" Width="SizeToHeader" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

The code behind:

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;

namespace Panel_OK
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void SearchClientEvent(object sender, TextChangedEventArgs e)
        {
            string outputInfo = "";
            string[] keyWords = SearchClientTB.Text.Split(' ');

            foreach (string word in keyWords)
            {
                if (word.Length >= 3)
                {
                    outputInfo = "SELECT * FROM [dbo].[Klient] WHERE [Imie] like '%" + word + "%' or [Nazwisko] like '%" + word + "%'";
                }
            }

            if (outputInfo.Length != 0)
            {
                try
                {
                    //Panel_OK.PanelOKDataSet panelOKDataSet = ((Panel_OK.PanelOKDataSet)(this.FindResource("panelOKDataSet")));
                    ////Load data into the table Klient. You can modify this code as needed.
                    //SqlCommand cmdSel = new SqlCommand(outputInfo);
                    //Panel_OK.PanelOKDataSetTableAdapters.KlientTableAdapter panelOKDataSetKlientTableAdapter = new Panel_OK.PanelOKDataSetTableAdapters.KlientTableAdapter();
                    //panelOKDataSetKlientTableAdapter.Fill(panelOKDataSet.Klient);
                    ////System.Windows.Data.CollectionViewSource klientViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("klientViewSource")));
                    ////klientViewSource.View.MoveCurrentToFirst();

                    SqlConnection connection = new SqlConnection("Data Source=DOM\\SQL2012EVAL;Initial Catalog=PanelOK;Integrated Security=True");
                    SqlCommand cmdSel = new SqlCommand(outputInfo, connection);
                    DataTable dt = new DataTable();
                    //DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(cmdSel);
                    da.Fill(dt);
                    if (dt != null) { klientDataGrid.DataContext = dt; }
                }
                catch (SqlException)
                {
                    MessageBox.Show("To run this example, replace the value of the " +
                        "connectionString variable with a connection string that is " +
                        "valid for your system.");
                }
            }
        }
    }
}
4

0 回答 0