Majid Kamali

Majid Kamali

  • NA
  • 290
  • 256.2k

Problem with Connecting to excel database

Jan 21 2011 9:04 PM
Hi.
I wrote a program that connects to an excel database but it makes an error in connection string line.
Here's the code:
                OleDbConnection Connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\DataBase2.xls;" +
                    "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";");
                OleDbDataAdapter Adapter = new OleDbDataAdapter("Select * from Sheet1", Connection);
                DataSet DS = new DataSet();
                DataTable DT = new DataTable();

                Adapter.Fill(DS);
                DT = DS.Tables[0];

                TextBox[,] TB = new TextBox[DT.Rows.Count, DT.Columns.Count];
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    for (int j = 0; j < DT.Columns.Count; j++)
                    {
                        TB[i, j] = new TextBox();
                        TB[i, j].Parent = this;
                        TB[i, j].Size = new Size(30, TB[i, j].Size.Height);
                        TB[i, j].Location = new Point(20 + j * TB[i, j].Size.Width, 20 + i * TB[i, j].Size.Height);
                        TB[i, j].Show();
                    }
                }

                int row = 0, col = 0;
                foreach (DataRow DR in DT.Rows)
                {
                    for (col = 0; col < DT.Rows.Count; col++)
                        TB[row, col].Text = DR[col].ToString();
                    row++;
                }
It makes a runtime error on lines 1 and 2 and says:
"Format of the Initialization string does not conform to specification starting at index 93."
What's the problem?

BTW, I use office 2007, does it makes problem?
Thanks :-)

Answers (2)