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

I am trying to make small program which reads excel file and write single column data of excel file to csv file. All things going on perfectly , dgv in form also shows all rows, but when i check csv file I found that not all rows copied. There are nearly 6000 rows in excel file but when data copied in csv file there are only 5706 rows last some rows are not copied error found that is "object reference could not set to the instance of object" at following bold line of code , needs sugessions .

thank you!

here is the code for read data from excel file,

C#
public void excel()
{
 StreamReader sr = new StreamReader("C:\\k.txt");
 string s = sr.ReadLine();
 OleDbConnection con = new OleDbConnection(
 "provider=Microsoft.Jet.OLEDB.4.0;data source='"+s+"'"
   + ";Extended Properties=Excel 8.0;");

 StringBuilder stbQuery = new StringBuilder();
 stbQuery.Append("SELECT Product FROM [Sheet1$]");
 OleDbDataAdapter adp = new OleDbDataAdapter(stbQuery.ToString(), con);

 DataSet dsXLS = new DataSet();
 adp.Fill(dsXLS);

   DataView dvEmp = new DataView(dsXLS.Tables[0]);

  dataGridView1.DataSource = dvEmp;
  }


here is the method for export data

C#
public void writeCSV(DataGridView gridIn, string outputFile)
{
 try
 {
    //test to see if the DataGridView has any rows
       if (gridIn.RowCount > 0)
       {
         //string value = "";
         DataGridViewRow dr = new DataGridViewRow();
          StreamWriter swOut = new StreamWriter(outputFile);

         //write header rows to csv
         for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
         {
           if (i > 0)
           {
               swOut.Write(",");
            }
           swOut.Write(gridIn.Columns[i].HeaderText);
          }

            swOut.WriteLine();

        //write DataGridView rows to csv
       for (int j = 0; j <= gridIn.Rows.Count - 1; j++)
       {
          if (j > 0)
          {
             swOut.WriteLine();
           }

          dr = gridIn.Rows[j];

       for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
       {
           if (i > 0)
           {
             swOut.Write(",");
           }
          string value = " ";
          value = dr.Cells[i].Value.ToString();// error line
          //replace comma's with spaces
          value = value.Replace(',', ' ');
          //replace embedded newlines with spaces
         value = value.Replace(Environment.NewLine, " ");

          swOut.Write(value);
          }
        }
         swOut.Flush();
         swOut.Close();
        }
      }
      catch { }
     }
Posted
Updated 29-May-15 20:59pm
v3

1 solution

Check it for null values.
If the cell contains a null - which is perfectly valid if the datasource is a null - then when you try to call ToString you will get an error.
So replace this:
C#
value = dr.Cells[i].Value.ToString();

with this:
C#
value = (dr.Cells[i].Value ?? "").ToString();
Which will replace null values with an empty string.
 
Share this answer
 

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