0

I'm using this code to parse the values and store them in List. The first row has names which are getting stored fine. But when storing values, only the second row is bring saved. I'm not sure what edit I need to make so that it parses all other rows as well.

Please see image and code below.

enter image description here

List<string> names = new List<string>();                   // List to store Key names
List<string> values = new List<string>();                  // List to store key values

using (StreamReader stream = new StreamReader(filePath))
{
    names = stream.ReadLine().Split(',').ToList();         // Seperate key names and store them in a List
    values = stream.ReadLine().Split(',').ToList();        // Seperate key values and store them in a list
}
4
  • 2
    So far your code does exactly what you describe - please edit your question so it is clear what you expect and what does not work. Type of values variable and lack of any iteration clearly indicate that code should just read one line of data from CSV. Commented Oct 24, 2013 at 4:43
  • 4
    Side note: Please consider to use existing CSV parsers... Your code handles escaped quotes completely wrong... Commented Oct 24, 2013 at 4:44
  • What its not doing is that its not storing values from row 3 onwards. It only stores row 2. Commented Oct 24, 2013 at 4:50
  • 1
    Why you don't use CsvHelper or FileHelpers? Commented Oct 24, 2013 at 4:57

2 Answers 2

3

See if something like this works better:

    // List to store Key names
    List<string> names = new List<string>();                                                 
    // List to store key values
    List<List<string>> values = new List<string>();                                               

    using (StreamReader stream = new StreamReader(filePath))
    {
        if(!stream.EndOfStream)
        {
            // Seperate key names and store them in a List
            names = stream.ReadLine().Split(',').ToList();
        }                     
        while(!stream.EndOfStream)
        {
            // Seperate key values and store them in a list
            values.Add(stream.ReadLine().Split(',').ToList());                 
        }
    }

This changes your values list to be a list of a list of strings so that each row will a list of string

While this probably isn't the best way to parse a .csv, if your data is consistent and the file format is strongly consistent you can probably get away with doing it like this. As soon as you try this with odd values, quoted strings, strings with commas, etc., you'll need a different approach.

Sign up to request clarification or add additional context in comments.

Comments

0

i have written the code for grid view make changes it to a list.I think it will help

protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string s = FileUpload1.FileName.Trim();
            if (s.EndsWith(".csv"))
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/data/" + s));
                string[] readText = File.ReadAllLines(Server.MapPath("~/data/" + s));
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
               // Array.Sort(readText);

                for (int i = 0; i < readText.Length; i++)
                {
                    if (i == 0)
                    {
                        string str = readText[0];
                        string[] header = str.Split(',');

                        dt.TableName = "sal";
                        foreach (string k in header)
                        {
                            dt.Columns.Add(k);
                        }
                    }
                    else
                    {
                        DataRow dr = dt.NewRow();

                        string str1 = readText[i];
                        if (readText[i] == ",,,,")
                        {
                            break;
                        }


                        string[] rows = str1.Split(',');
                        if (dt.Columns.Count == rows.Length)
                        {

                            for (int z = 0; z < rows.Length; z++)
                            {
                                if (rows[z] == "")
                                {
                                    rows[z] = null;

                                }

                                dr[z] = rows[z];
                            }
                            dt.Rows.Add(dr);


                        }
                        else
                        {
                            Label1.Text = "please select valid format";
                        }
                    }


                }

                //Iterate through the columns of the datatable to set the data bound field dynamically.
                ds.Merge(dt);
                Session["tasktable"] = dt;
                foreach (DataColumn col in dt.Columns)
                {
                    BoundField bf = new BoundField();

                    bf.DataField = col.ToString();
                    bf.HeaderText = col.ColumnName;
                    if (col.ToString() == "Task")
                    {
                        bf.SortExpression = col.ToString();
                    }
                    GridView1.Columns.Add(bf);

                }
                GridView1.DataSource = ds;
                GridView1.DataBind();


            }
            else
            {
                Label1.Text = "please select a only csv format";
            }

        }
        else
        {
            Label1.Text = "please select a file";
        }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.