2

Hi i have CSV upload code in .net

In C#

 string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=D:\\csv;Extensions=asc,csv,tab,txt;Persist Security Info=False";
            DataSet ds;
            using (OdbcConnection oConn = new OdbcConnection(strConnString))
            {
                using (OdbcCommand oCmd = new OdbcCommand())
                {
                    oCmd.Connection = oConn;
                    oCmd.CommandType = System.Data.CommandType.Text;
                    oCmd.CommandText = "select * from [my.csv]";

                    OdbcDataAdapter oAdap = new OdbcDataAdapter();
                    oAdap.SelectCommand = oCmd;

                    ds = new DataSet();
                    oAdap.Fill(ds, "my");
                    oAdap.Dispose();                     
                    ds.Dispose();
                }
            }

In .VB

  Dim strConnString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=D:\\csv;Extensions=asc,csv,tab,txt;Persist Security Info=False"

        Dim lOdbcConnection As New OdbcConnection(strConnString)
        '            lOdbcConnection.ConnectionString = strConnString
        'lOdbcConnection.Open()
        Using lOdbcCommand As New OdbcCommand, lOdbcDataAdapter As New OdbcDataAdapter
            lOdbcCommand.Connection = lOdbcConnection
            lOdbcCommand.CommandType = System.Data.CommandType.Text
            lOdbcCommand.CommandText = "select * from [my.csv]"

            lOdbcDataAdapter.SelectCommand = lOdbcCommand
            Dim ds As New DataSet()
            lOdbcDataAdapter.Fill(ds, "my")
            ds.Dispose()

            lOdbcDataAdapter.Dispose()

In C# is working fine But in .VB its giving error when filling the dataset .-

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

what I am doing wrong?

Scorpion
  • 4,495
  • 7
  • 39
  • 60
Dilip
  • 697
  • 6
  • 16
  • 35

1 Answers1

1

You don't need to escape the \ character in VB.NET strings so "Dbq=D:\\csv" should be "Dbq=D:\csv". That's why your data source isn't found.

jfrankcarr
  • 481
  • 3
  • 6
  • Other than the double \, the only other problem in your posted code that it isn't complete, lacking a Dim for the first variable and an End Using statement at the end. It compiles OK when these are corrected and runs OK as well (in VS2010) as long as the code points to a valid path and CSV filename. The error you're getting indicates that the CSV file can't be found or read so I'd trace through the code and make sure it's seeing the file OK. Also, make sure it isn't being locked by another process. – jfrankcarr Sep 11 '13 at 13:28
  • 1
    hi error is not in code actual problem with driver as in another machine working fine . – Dilip Sep 16 '13 at 11:05