填充datagrid的函数中的NullReferenceException

时间:2012-09-09 17:22:37

标签: c# wpf connection nullreferenceexception

我浪费整个星期天在C#WPF应用程序中对datagrid进行简单填充。 总而言之,我使它像所有Web示例一样,但代码(如下)抛出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;

奇怪的是编译器在调试模式下通过代码,即使它是由textbox的textchange属性触发的事件。

Xaml声明如下:

<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>

背后的代码:

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.");
                }
            }
        }
    }
}

0 个答案:

没有答案