Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

I tried to establish a connection between ms access 2007 and wpf .

When i debug the code i just have the form appear.

No errors and no output also.

I am having a doctors_records table.
using WPF -visual studio 2010.
Wpf main window - has four text boxes and 4 labels.

Please find the following code and please let me know where i am going wrong.

Code:
C#
     public partial class MainWindow : Window
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SR0\Documents\Doctors.accdb");
        public MainWindow()
        {
            InitializeComponent();
            Bind();

        }
        private void Bind()
        {
            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("Select * from Doctors_Records", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
        }
    }
}
Posted
Updated 18-Oct-12 3:04am
v2
Comments
[no name] 18-Oct-12 8:55am    
You just have the window appear because you are not doing anything with the data.

you need to use Data Binding.

check this out

WPF Data Binding - Part 1[^]
 
Share this answer
 
Here's what I did:

In Window.xaml:

Window>
Grid x:Name="root">
DataGrid ItemsSource="{Binding}" >

/DataGrid>
/Grid>
/Window>

(I cannot type the '<' for some reason in the above code... But I only have a DataGrid in my window wrapped in a grid named 'root')

====================

In Window.xaml.cs:

public partial class MainWindow : Window
{

DataTable dt = new DataTable();
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\username\Documents\Access\SampleData.accdb");
public MainWindow()
{
InitializeComponent();
GetData();

}
private void GetData()
{
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * from Table1", con);

da.Fill(dt);

root.DataContext = dt;

con.Close();
}


}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900