Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

            string extension = Path.GetExtension(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0;");

        using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection())
        {
            switch (extension)
            {
                case ".xls":
                    string xlsconStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0;";
                    con.ConnectionString = xlsconStr;
                    break;

                case ".xlsx":
                case ".xlsm":
                    string xlsxconStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 12.0;";
                    con.ConnectionString = xlsxconStr;
                    break;
            }
                using (System.Data.OleDb.OleDbCommand oconn = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + listBox1.SelectedIndex.ToString() + "$]", con))
                {
                    // **There will be an error here**
                    // **The ConnectionString property has not been initialized.**
                    con.Open();

                    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(oconn);
                    System.Data.DataTable data = new System.Data.DataTable();
                    adapter.Fill(data);
                    dataGridView1.DataSource = data;
                }
        }

What's wrong with the code? My purpose is to draw all database on selectedItem on listBox and whenever I click it will populate the dataGridView

BTW, the selected item was from the MSExcel worksheetName

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
222 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your code needs a very little fix:

string extension = Path.GetExtension(openFileDialog1.FileName);

Previously your GetExtension was trying to find an extension on your connection string, witch is impossible for the method, because is a connection string and not a path!

So your code fixed should look like this:

    string extension = Path.GetExtension(openFileDialog1.FileName);

    using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection())
    {
        switch (extension)
        {
            case ".xls":
                string xlsconStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0;";
                con.ConnectionString = xlsconStr;
                break;

            case ".xlsx":
            case ".xlsm":
                string xlsxconStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 12.0;";
                con.ConnectionString = xlsxconStr;
                break;
        }
            using (System.Data.OleDb.OleDbCommand oconn = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + listBox1.SelectedIndex.ToString() + "$]", con))
            {
                con.Open();

                System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(oconn);
                System.Data.DataTable data = new System.Data.DataTable();
                adapter.Fill(data);
                dataGridView1.DataSource = data;
            }
    }

Now your code will enter the switch case statement in order to initialize you connection string ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...