none
Problem on reading updated csv files using c# RRS feed

  • Question

  • Hi Friends,
    I have a problem in reading CSV files.
    My customer sent me a CSV files and I am reading this file as;

     

    Code Block

    string path = @"C:\TEST\";
    System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"text;HDR=No;IMEX=1;FMT=Delimited\"");
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT * FROM sample.csv", conn);

    cmd.Connection = conn;
    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(cmd);
    System.Data.DataSet ds = new DataSet();
    conn.Open();
    adapter.Fill(ds);

    conn.Close();

     

     

     

    There is no problem on reading CSV file. But, I am changing the csv file's 1.row and 2.column. Then,
    I when I have read this file with above code, the value of;

    ds.Tables[0].Rows[0][1] is equal System.DbNull

    If I don't change the value of 1.row and 2.column it is real value but after changing it is System.DbNull.

    How can I solve my problem. I need a solution that If the contents of CSV file is changed I should get the new content of CSV file. Please help me.

    Thanks.

    Thursday, October 11, 2007 3:04 PM

Answers

  •  

    Hi again,

    Thank you for helps.

    I have found the solution. When I have updated my cell, its type is also updated. Then the registry settings for Text file try to recognize type of all column. After all, when the major type of column is different my cell type, my cell is equal to DbNull.

    So, I have solved my problem as giving schema.ini file and I have configured all the columns as Text;

    Code Block

    [MyFile.csv]
    Format=Delimited(,)
    ColNameHeader=True
    MaxScanRows=0
    Col1=FieldName1 Text
    Col2=FieldName2 Text
    Col3=FieldName3 Text
    Col4=FieldName4 Text
    CharacterSet=ANSI

     

     

    Tuesday, October 16, 2007 9:29 AM

All replies

  • How are you changing the value of the column in the row? Can you post a sample of your code?

     

    Friday, October 12, 2007 12:22 PM
  •  

    Hi Paul,

    Thanks for your answer,

    I am changing the csv file manually, not with code.

    In csv file the delimeter is comma, so, I have changed the "Standart and Formats" to English(United States) under Regioanal and Language Options->Regional Options

    After I have changed the csv file manually, I can see that file is changed when I reopened file.

    It is so interesting that the file's changed column is equal to System.DbNull

    I want to send my csv file but in this platform I can not attach my csv file. If it is possible I want to send this file to your e-mail.

    Thanks.

    Saturday, October 13, 2007 10:57 AM
  • Hi,

     

    Have you thought about opening and parsing the file yourself rather than using OleDb?  In any case, you can post a few records of your file here for Paul to take a look at.  Remember to "sanitize" anything that might be sensitive.

    Monday, October 15, 2007 2:30 PM
  •  

    Hi again,

    Thank you for helps.

    I have found the solution. When I have updated my cell, its type is also updated. Then the registry settings for Text file try to recognize type of all column. After all, when the major type of column is different my cell type, my cell is equal to DbNull.

    So, I have solved my problem as giving schema.ini file and I have configured all the columns as Text;

    Code Block

    [MyFile.csv]
    Format=Delimited(,)
    ColNameHeader=True
    MaxScanRows=0
    Col1=FieldName1 Text
    Col2=FieldName2 Text
    Col3=FieldName3 Text
    Col4=FieldName4 Text
    CharacterSet=ANSI

     

     

    Tuesday, October 16, 2007 9:29 AM