0

I have written the code to read the csv file(which include 4 rows)
and the application is running sucessfully, while writting to the file it writes 2nd and 4th row to the file
I am not getting why its happening.
Can anyone help me for the same.
following is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.IO;

namespace Test1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Uploadbutton_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            filenametextBox.Text = openFileDialog1.FileName;

        }

        private void save_to_DBbutton_Click(object sender, EventArgs e)
        {
            
            if (openFileDialog1.FileName == "")
            {
                MessageBox.Show("File Not Selected");
            }
            StreamReader sr = new StreamReader(openFileDialog1.FileName,Encoding.GetEncoding(1250));
            StreamWriter SW;
            SW = File.CreateText("D:\\CSV\\csv.txt");

            string strline = "";
            string str1 = "";
            string[] _values = null;
            
            while ((strline = sr.ReadLine()) != null)
            {

                strline = sr.ReadLine();
                _values = strline.Split(';');
                foreach (string str in _values)
                {
                    str1 = str + ",";
                }
                str1 = str1.Trim(',');
                SW.WriteLine(str1);
            
            }            sr.Close();
            SW.Close();
            MessageBox.Show("Sucessfully Retrieved and file has been created");
            this.Close(); 
        }
    }
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ketsuekiame
0
while ((strline = sr.ReadLine()) != null)
{
    strline = sr.ReadLine();
    /* Code and stuff... */
}

These are lines 46 to 50 in your code.

Now I've pointed it out, it should be easy to see why it only reads lines 2 and 4 from your csv file.

Edited by Ketsuekiame: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.