Hi All

I am trying to connect to access 07 via a windows app using msvs 05 in c#. I have the code which ive adapted from another working app but its now not working. Initialy the form should open an display the data rows in a table called events and then i want to add, change delete. For now i just wanna get it to connect and show the data. Unfortunately this causes an error saying

System.Data.OleDb.OleDbException was unhandled, Message="Not a valid file name."

I am new to c# an a little confused as to what i am doing wrong could someone point me in the right direction please!!
Apologies for the comments its helpin me rember what am doing while i learn lol

Heres the code ive bolded the error line

public partial class Form2 : Form
    {
        public static string connectionString =
               "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
               + "data source = " + Application.StartupPath + //(Location) where to find db file in bin file
"\\Olympics.accdb";

        //delcalring private variabe
        private OleDbDataAdapter dataAdapter;
        private DataSet dataSet;
        private DataTable dataTable;
        private OleDbConnection conn;

        //*** End ADO.Net objects ***

        // Database objects

        private static string EventName; //creates object in memory 1 instance used anytime
        private static string EventDate;

        public Form2()
        {
            InitializeComponent();
            // Default select command on the Events table
            string commandstring = "select * from Events";//select everything from table

            // The link between the sql command and the database connection

            dataAdapter = new OleDbDataAdapter(commandstring, connectionString);//gives everythin from table to dataAdapter and use connection string

            //Define insert, update, and delete sql commands to use.
            //BuildCommands();

            //Declare and fill the in-memory dataset from the database
            dataSet = new DataSet();
            dataSet.CaseSensitive = true;
            [B]dataAdapter.Fill(dataSet, "Events");[/B]

            // Show all rows in the listbox
            Fill_1b();
        }
        private void Fill_1b()
        {
            dataTable = dataSet.Tables["Events"];
            listBox1.Items.Clear();

            foreach (DataRow dataRow in dataTable.Rows)
            {
                LoadBuffers(dataRow);
                listBox1.Items.Add(EventName + "\t\t" + EventDate);
            }
        } // *** Fill_1b ***

        private void LoadBuffers(DataRow prow)
        {
            EventName = prow["EventName"].ToString().Trim();
            EventDate = prow["EventDate"].ToString().Trim();
        } //*** LoadBuffers ***

Hi,

The problem is with the connection string that you have provided.

Make the connection string to:

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "" + Application.StartupPath + "\\Olympics.accdb";
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();

eliminate the "data source = " part from the connection string since you have already defined.

Good luck.

Hi,

Thanx works a treat now

cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.