0

Hi I am trying to connect the WPF datagrid to a Access database but i am not able to get any update and no error when i try to call the data.

XML code

<Window x:Class="SRT.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="627.49" Width="930.777">
<Grid Margin="0,0,-8,11">
    <TabControl HorizontalAlignment="Left" Height="583" VerticalAlignment="Top" Width="921">
        <TabItem Header="TabItem">
            <Grid Background="#FFE5E5E5" Margin="0,0,6,0">
                <ComboBox HorizontalAlignment="Left" Height="28" Margin="11,178,0,0" VerticalAlignment="Top" Width="128" Name="date"/>
                <ComboBox HorizontalAlignment="Left" Height="28" Margin="146,178,0,0" VerticalAlignment="Top" Width="201" Name="Agent_Name"/>
                <ComboBox HorizontalAlignment="Left" Height="28" Margin="358,178,0,0" VerticalAlignment="Top" Width="165" Name="Status"/>
                <Button Content="Refresh" HorizontalAlignment="Left" Height="28" Margin="528,178,0,0" VerticalAlignment="Top" Width="110" x:Name="Refresh" Click="Refresh_Click"/>
                <Button Content="Get Data" HorizontalAlignment="Left" Height="28" Margin="643,178,0,0" VerticalAlignment="Top" Width="110" x:Name="getdata" Click="getdata_Click"/>
                <Label Content="Date" HorizontalAlignment="Left" Height="27" Margin="11,146,0,0" VerticalAlignment="Top" Width="128"/>
                <Label Content="SRT Agent Name" HorizontalAlignment="Left" Height="27" Margin="146,146,0,0" VerticalAlignment="Top" Width="201"/>
                <Label Content="Case Status" HorizontalAlignment="Left" Height="27" Margin="358,146,0,0" VerticalAlignment="Top" Width="165"/>
                <Frame Content="Update" HorizontalAlignment="Left" Height="136" Margin="11,10,0,0" VerticalAlignment="Top" Width="743"/>
                <TextBox HorizontalAlignment="Left" Height="30" Margin="25,79,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="97"/>
                <ComboBox HorizontalAlignment="Left" Margin="127,79,0,0" VerticalAlignment="Top" Width="162" Height="30"/>
                <ComboBox HorizontalAlignment="Left" Height="30" Margin="294,79,0,0" VerticalAlignment="Top" Width="121"/>
                <Button Content="Update" HorizontalAlignment="Left" Height="30" Margin="420,79,0,0" VerticalAlignment="Top" Width="96"/>
                <Label Content="ID Number" HorizontalAlignment="Left" Height="27" Margin="25,47,0,0" VerticalAlignment="Top" Width="97"/>
                <Label Content="Updated Status" HorizontalAlignment="Left" Height="27" Margin="127,47,0,0" VerticalAlignment="Top" Width="162"/>
                <Label Content="Callback Attempts" HorizontalAlignment="Left" Height="27" Margin="294,47,0,0" VerticalAlignment="Top" Width="121"/>
                <TextBox HorizontalAlignment="Left" Height="27" Margin="125,114,0,0" TextWrapping="Wrap" Text="Add Database Path here" VerticalAlignment="Top" Width="391" Name="path"/>
                <Label Content="Path" HorizontalAlignment="Left" Height="27" Margin="23,114,0,0" VerticalAlignment="Top" Width="97"/>
                <DataGrid HorizontalAlignment="Left" Height="158" Margin="5,217,0,0" VerticalAlignment="Top" Width="890" Name="alldata" AutoGenerateColumns="True" SelectionChanged="alldata_SelectionChanged"/>
            </Grid>
        </TabItem>
        <TabItem Header="TabItem">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
    </TabControl>
</Grid>

C# code

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;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;

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

    }

    private void Refresh_Click(object sender, RoutedEventArgs e)
    {
        OleDbConnection con2 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Sumeet\\Desktop\\Sitel Project\\Support_Tracker.accdb;Persist Security Info=False;");
        OleDbCommand cmd2 = new OleDbCommand();
        OleDbDataAdapter da2 = new OleDbDataAdapter();
        DataTable dt2 = new DataTable();
        String fselect2 = null;
        fselect2 = "Select DISTINCT Date from SRT";
        cmd2 = new OleDbCommand(fselect2, con2);
        da2 = new OleDbDataAdapter(cmd2);
        da2.Fill(dt2);

        date.ItemsSource = dt2.DefaultView;
        date.DisplayMemberPath = dt2.Columns["Date"].ToString();
        con2.Close();

        OleDbConnection con3 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Sumeet\\Desktop\\Sitel Project\\Support_Tracker.accdb;Persist Security Info=False;");
        OleDbCommand cmd3 = new OleDbCommand();
        OleDbDataAdapter da3 = new OleDbDataAdapter();
        DataTable dt3 = new DataTable();
        String fselect3 = null;
        fselect3 = "Select DISTINCT Agent_Name from SRT";
        cmd3 = new OleDbCommand(fselect3, con3);
        da3 = new OleDbDataAdapter(cmd3);
        da3.Fill(dt3);

        Agent_Name.ItemsSource = dt3.DefaultView;
        Agent_Name.DisplayMemberPath = dt3.Columns["Agent_Name"].ToString();
        con3.Close();

        OleDbConnection con4 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Sumeet\\Desktop\\Sitel Project\\Support_Tracker.accdb;Persist Security Info=False;");
        OleDbCommand cmd4 = new OleDbCommand();
        OleDbDataAdapter da4 = new OleDbDataAdapter();
        DataTable dt4 = new DataTable();
        String fselect4 = null;
        fselect4 = "Select DISTINCT Status from SRT";
        cmd4 = new OleDbCommand(fselect4, con4);
        da4 = new OleDbDataAdapter(cmd4);
        da4.Fill(dt4);

        Status.ItemsSource = dt4.DefaultView;
        Status.DisplayMemberPath = dt4.Columns["Status"].ToString();
        con4.Close();


    }

    private void getdata_Click(object sender, RoutedEventArgs e)
    {

        OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Sumeet\\Desktop\\Sitel Project\\Support_Tracker.accdb;");
        OleDbCommand cmd1 = new OleDbCommand();
        OleDbDataAdapter da1 = new OleDbDataAdapter();
        DataTable dt1 = new DataTable();
        String fselect1 = null;
        fselect1 = "Select * from Org";
        cmd1 = new OleDbCommand(fselect1, con1);
        da1 = new OleDbDataAdapter(cmd1);
        da1.Fill(dt1);

        alldata.DataContext = dt1.DefaultView;  

    }

    private void alldata_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }



}

}

It does the combobox perfectly but not the datagrid not sure what i am doing wrong here as there is no error displayed Kindly help

Sumeet Pujari
  • 181
  • 1
  • 5
  • 19

1 Answers1

1

My first thought is: I would encapsulate the data into objects, which implement the INotification interface. Then put the objects in an observable collection. Then use this collection as the data source.

Michael
  • 938
  • 1
  • 10
  • 34
  • Actually i am a complete Newbie when it comes o this can you code a example for me with my code i would really be thankful. – Sumeet Pujari May 05 '13 at 08:47
  • Just google your way through C# Notification Objects or the INotification interface, and what it's for. You will find tons of examples in the net. If you use the entity framework, and not an OleDbConnection, to access your database, you don't even have to write these objects on your own. – Michael May 05 '13 at 08:58
  • http://stackoverflow.com/questions/11432488/how-to-use-entity-framework-for-ms-access-database – Michael May 05 '13 at 09:01
  • Hey Michel the above code worked before without any issue i accidentally deleted the project and i am lost now in the XML part bcoz i am dead sure i am doing right with C# i am not sure about XML part here. – Sumeet Pujari May 05 '13 at 09:21
  • I also have the working published exe i wish somehow i code decode it to show me the code my issue would be solved. – Sumeet Pujari May 05 '13 at 09:22
  • @Michael WHat do you mean by "encapsulate" it into data objects? Do you mean create a class to hold 1 row of data or all the data coming back? Can you please provide a link. Your link leads to another link that chains to another... and before you know it Im lost – software is fun Mar 06 '15 at 16:39
  • @SoftwareIsFun - Yes, I mean just define plain pure CLR business objects, that hold the data. The business objects should implement the INotification interface. Put these objects in an observable collection and set this observable collection as the datasource of the grid. Everytime any business object is changed, the change will be displayed in the data grid. This is the most common approach, when displaying data in a datagrid. – Michael Apr 13 '15 at 19:18