Click here to Skip to main content
14,825,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am importing ".xls/.xlsx/.csv" file using switch case and ".xls/.xlsx" works fine but ".csv" file shows me the error as follows
"'*.csv' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides." 

in oconn.open().

Could someone please help me.

I'm using the following code:

C#
switch (Extension)
        {
            case ".xls": //Excel 97-03
                conStr = String.Format(ConfigurationManager.ConnectionStrings["WorkingConnectionStringforxls"].ConnectionString + (char)34, path, (char)34);
                break;
            case ".xlsx": //Excel 07
                conStr = String.Format(ConfigurationManager.ConnectionStrings["WorkingConnectionStringforxlsx"].ConnectionString + (char)34, path, (char)34);
                break;
            case ".csv":  //CSV file
                conStr = String.Format(ConfigurationManager.ConnectionStrings["WorkingConnectionStringforcsv"].ConnectionString + (char)34, path, (char)34);
                break;
        }
        oconn = new OleDbConnection(conStr);
        oconn.Open();
        OleDbCommand ocmd1 = new OleDbCommand("select count(*) from [Sample input sheet$]", oconn);
        Int32 count = (Int32)ocmd1.ExecuteScalar();
        if (count == 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "errale", "alert('The excel sheet is empty');", true);
                }
        else
        {
            GridView1.DataSource = ExtractData();
            GridView1.DataBind();
            UpdateMaterial.Visible = true;
                    }
        oconn.Close();



This is the connection string I'm using in web.config:

XML
<add name="WorkingConnectionStringforcsv" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties={1}text;HDR=NO;FMT=Delimited;" />


Thanks in advance.
Posted
Updated 2-Jan-12 6:50am
v7

Can you please tell us that what is your connection string.
C#
ConfigurationManager.ConnectionStrings["WorkingConnectionStringforcsv"]

May be there is problem in that
   
v3
Comments
skkworld 2-Jan-12 5:50am
   
This is the connection string I'm using in web.config
<add name="WorkingConnectionStringforcsv" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties={1}text;HDR=NO;FMT=Delimited;"></add>
Have a look at http://www.connectionstrings.com/excel[^] and ensure you have got the right connection string for the right Excel version.
   
Comments
NandaKumer 16-Jan-12 13:09pm
   
good one
go through the link
here you can find connectionstring for TextFile(CSV)
Here[^]
   
Comments
NandaKumer 16-Jan-12 13:09pm
   
good one

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900