0

I am working on a C# Application and needs to get data from Excel Sheet named SMS. I tried the below code but it gives me the subjected error that is "Error Loading Excel File Kindly Check Worksheet Name".

Here is the code:

private void button3_Click_1(object sender, EventArgs e)
{

    int rows_counting, column_counting1 = 0;

    OpenFileDialog dialog = new OpenFileDialog { };

    dialog.Filter = "SMS Sending File(*.xlsx;*.xls)|*.xlsx;*.xls";

    dialog.Title = "Select Excel File For SMS";

    DialogResult dlgresult = dialog.ShowDialog();

    if (dlgresult == DialogResult.Cancel)
    {
        MessageBox.Show("You Press Cancelled :-) !!!");
    }
    else
    {
        string sms_filename = dialog.FileName;

        if (System.IO.File.Exists(sms_filename))
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", sms_filename);

                string query = String.Format("select * from [{0}$]", "SMS");

                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

                dataSet = new DataSet();

                dataAdapter.Fill(dataSet);

                dataGridView1.DataSource = dataSet.Tables[0];

                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

                rows_counting = dataGridView1.RowCount - 1;                                            

                column_counting1 = dataGridView1.ColumnCount;

                if (column_counting1 < 2 || column_counting1 > 2)
                {
                    MessageBox.Show("Kindly Check Column Count in Excel Sheet !!!\r\n\nThere Should Be Only Two Columns in Sheet Like Below\r\n\nCELL NUMBER | MESSAGE", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                            
                    return;
                }

                if (    dataGridView1.Columns[0].Name.ToString().ToUpper() == "CELL NUMBER" &&
                        dataGridView1.Columns[1].Name.ToString().ToUpper() == "MESSAGES")

                {
                    label25.Text = "Total SMS In Excel File " + rows_counting;
                    MessageBox.Show("Data Imported Successfully...!!!\r\n\nCheck Imported Values & SEND SMS.....!!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    button7.Enabled = true;
                    button4.Enabled = true;

                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    MessageBox.Show("Column Names Are Not In Specified Format !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }


            }
            catch (Exception E6)
            {
                MessageBox.Show("Error Loading Excel File\r\n\nKindly Check Worksheet Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }




        }
    }


}
Naveed
  • 59
  • 2
  • 13
  • 1
    That is the general message that you are showing for any exception(As per your code). Can you tell in your question what is the exception. Have you debugged your code ? – Vandita Mar 30 '17 at 06:44
  • Thanks for your replies the solution was this post. [http://stackoverflow.com/questions/6649363/microsoft-ace-oledb-12-0-provider-is-not-registered-on-the-local-machine](http://stackoverflow.com/questions/6649363/microsoft-ace-oledb-12-0-provider-is-not-registered-on-the-local-machine) – Naveed Mar 30 '17 at 07:23

1 Answers1

0

Please confirm your worksheet name is SMS. Also check there is no blank spaces in the worksheet name.

Santhosh
  • 729
  • 7
  • 19
  • Yes my Worksheet name is SMS and there is no space in Worksheet name. The exception is Microsoft.ACE.OLEDB.12.0 is not registered on local computer. – Naveed Mar 30 '17 at 06:52