locked
Error Invalid Argument RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code . It is giving Invalid argument

    switch (strExtensionName)
    {
    case ".xls":
    excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";";
    break;
    case ".xlsx":
    excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";";
    break;
    }

    OleDbConnection cnnExcel = new OleDbConnection(excelConnectionString);
    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", cnnExcel);
    cnnExcel.Open();

    Thanks

    Tuesday, September 11, 2018 3:54 PM

Answers

  • User-893317190 posted

    Hi JagjitSingh,

    Please check your excelConnectionString to see whether it is in right format. If it is not, please debug to see what causes the problem.

    I run the code below  and meet the same problem. The cause is connectionString.

       string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "" + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";";
              
    
                    OleDbConnection cnnExcel = new OleDbConnection(excelConnectionString);
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", cnnExcel);
                    cnnExcel.Open();

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 6:23 AM

All replies

  • User753101303 posted

    Hi,

    It happens when creating the OleDbConnection ? Could it be that strExtensionName is not ".xls" or ".xlsx" (uppercase or maybe ".xlsm" or some o) in which case the connection string could be left empty ?

    Tuesday, September 11, 2018 5:58 PM
  • User-1716253493 posted

    Try change this to avoid case error

    switch (strExtensionName.ToLower())

    Ensure db operation only if it is xls or xlsx, use default case to show wrong extension message and skip db operation

    Wednesday, September 12, 2018 12:57 AM
  • User-893317190 posted

    Hi JagjitSingh,

    Please check your excelConnectionString to see whether it is in right format. If it is not, please debug to see what causes the problem.

    I run the code below  and meet the same problem. The cause is connectionString.

       string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "" + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";";
              
    
                    OleDbConnection cnnExcel = new OleDbConnection(excelConnectionString);
                    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", cnnExcel);
                    cnnExcel.Open();

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 6:23 AM