Click here to Skip to main content
15,170,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to connect to the access database 2007 and WPF 2010.

I am getting an exception know XAML Parse Exception was unhandled .The invocation of the constructor on type 'Login_Details.MainWindow' that matches specified binding constraints threw an exception Line Number.3 and Line Number.9 position.
C#
public MainWindow()
        {
            InitializeComponent();
            connection();
        }
        public void connection()
        {
            string connectionString = null;
            OleDbConnection cnn;
            OleDbCommand cmd;
            string sql = null;
            connectionString = "@Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SR010\\Documents\\Login.accdb";
            sql = "Select * from Login";
            cnn = new OleDbConnection(connectionString);
            try
            {
                cnn.Open();
                MessageBox.Show("Connection Opened");
                cmd = new OleDbCommand(sql, cnn);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                cnn.Close();
                MessageBox.Show("Execute Non Query in OleDbConnection executed !!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot open connection !" + ex.ToString());
            }
        }
    }
}

Please suggest what has to be done

Thanks in advance
Posted
Updated 27-Oct-12 23:08pm
v2
Comments
Clifford Nelson 29-Oct-12 19:35pm
   
You are getting a xaml error, but have not included the xaml. You are not going to get a good answer without providing the xaml

1 solution

You cann't create a connection to a database in the constructor. My debugger told me there's a error in the line
C#
cnn = new OleDbConnection(connectionString);

So, you need to create a connection when the window is loaded, or on a button click, but not in the constructor.
   

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