C# .NET - .csv file export and import in windows forms

Asked By vijaya saladi on 01-Jun-12 01:02 AM
Hai.,

                    I have a application in that one data grid and 2 buttons are there ,1 button is for import the data,and second one is for export the data from data grid to .csv file.The data grid has 4 columns so when ever the user click the import data, the data in the file will come to the appropriate columns in the data grid.and also when user clicks export the data just stored in the new csv file and asking for name to save the file.So please any one help me for this.

                          I already developed one function to import the data,it import the data but it completely overwrite the data grid including the columns headers also.so please provide the solution for this.


Thank you.,
Vijaya.S
Jitendra Faye replied to vijaya saladi on 01-Jun-12 01:04 AM
This is code to export CVS file data to database,

try this code-

Here is the working code 

string strProvier = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Employees.mdb";
 OleDbConnection con = new OleDbConnection(strProvier);
 OleDbCommand cmd = new OleDbCommand();
 int effectedRow = 0;
 int lineCounter = 0;
 
 con.Open();
 
 if ((con.State.ToString() == "Open"))
{
  
   StreamReader stReader = new StreamReader("Employees.csv");
   string[] strRowData = null;
  
   while (stReader.Peek() >= 0)
   {
     lineCounter = lineCounter + 1;
     strRowData = stReader.ReadLine().Split(",");
    
     try {
       cmd.CommandText = "INSERT INTO tbl_employees(emp_first_name,emp_last_name,emp_salary) VALUES ('" + strRowData(0) + "','" +                   strRowData(1) + "','" + strRowData(2) + "')";
       cmd.Connection = con;
       effectedRow = cmd.ExecuteNonQuery();
      
       if ((effectedRow == -1)) {
         //  Messagebox.Show("Line: " + lineCounter + " Error"); 
       }
       else {
          // Messagebox.Show("Line: " + lineCounter + " Executed Successfully");
        
       }
     }
     catch (OleDbException er) {
       Messagebox.Show("Line: " + lineCounter + " Error: " + er.Message);
      
     }
   }
   stReader.Close();
   con.Close();
 }
 else {
   Messagebox.Show("Not Connected To Database");
 }


This file should contain any separator.

TRy this  and let me know.
Somesh Yadav replied to vijaya saladi on 01-Jun-12 01:23 AM
http://doc.openerp.com/v6.0/book/8/8_20_Config/8_20_Config_import_export.html