Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have csv file which is constantly appended by some hardware controller.
This csv I have to stream it in Datagrid.

I have done streaming on timer, which is working fine. Timer time is set to 1 seconds.

so whats is happening that, the file written by hardware/controller can be in milliseconds or seconds...etc.

my program reads csv after every 1 seconds....and if the time overlaps of reading(from s/w) and writing(from h/w)...then exception comes and program cant read further.

Can any body help me ?
Posted

1 solution

Use a try...catch block around the access code to pick up the "File In Use" Exception, and if it fails abandon and try again a second later.
C#
try
   {
   ...
   }
catch (IOException ex)
 
Share this answer
 
Comments
Pdeveloper 15-Jan-14 6:18am    
I have done this also..when it is in try-catch block and if the file is not use in that second...it reads file and again if while reading the file is in use with h/w.. then this does not work :(
OriginalGriff 15-Jan-14 6:21am    
"does not work" in what way?
Pdeveloper 15-Jan-14 6:21am    
no..
Pdeveloper 15-Jan-14 6:27am    
here is my code

public DataSet LoadCSVToGrid()
{
DataSet ds = new DataSet();

if (File.Exists( @"C:\\Count_06.csv"))
{
try
{
// Creates and opens an ODBC connection
string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + dirCSV.Trim() +";Extensions=asc,csv,tab,txt; Persist Security Info= false";
string sql_select;
OdbcConnection conn;
conn = new OdbcConnection(strConnString.Trim());
conn.Open();

//Creates the select command text

sql_select = "select * from [" + fileNaneCSV + "]";

//Creates the data adapter
OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);

//Fills dataset with the records from CSV file
obj_oledb_da.Fill(ds, "csv");
DataTable dt = new DataTable();
dt = ds.Tables["csv"];
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
string temp = dt.Rows[i][0].ToString();
}
//closes the connection
conn.Close();
}
catch (Exception e) //Error
{
MessageBox.Show(e.Message, "Error - LoadCSV", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("The csv File does not exists");
}
return ds;
}


AND then this dataset is passed to datagrid.
OriginalGriff 15-Jan-14 6:54am    
Ah! You aren't reading the CSV yourself, you are getting SQL to do it...so you won't get an IOException, you will get an SqlException instead. - you could trap that, but I'd copy the CSV in my C#, and pass the copied file to SQL - that way SQL Server can always access the file, and it's your program that is responsible for getting it when it's free.

Mind you, I wouldn't use SQL to access the file anyway - there are quicker, easier solutions to reading CSV data:
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

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