Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.29/5 (4 votes)
Hi, I have a class named DataSet
C#
public class DataSet
    {
         private string _title;
        private List<ImageAssociation> _associated_images = new List<ImageAssociation>();
 
        
        public string time_start = "Started";
        
        public string duration { get; set; }
        
        public string title
        { 
            get { return _title; }
            set
            { 
                int length = value.Length > 150 ? 150 : value.Length;
                _title = value.Substring(0, length);
            }
        }
 
        
        public string description { get; set; }
 
        
        public string integration_id { get; set; }
 
        
        public string auction_code { get; set; }
 
        
        public string viewing_information { get; set; }
 
        
        public AuctionPublicationStatus publication_status { get; set; }
 
        public string currency_code { get; set; }
 
        
        public string location_name { get; set; }
 
       
        public string location_description { get; set; }
 
        
        public List<ImageAssociation> associated_images { get { return _associated_images; } }
    }
    }


ImageAssociation class as
C#
public partial class ImageAssociation
    {
        
        public string image_id { get; set; }
        
        public string caption { get; set; }
    }


Now I want to convert values in this class file to a csv file My code for the csv file write is as follows

C#
 protected void Button1_Click(object sender, EventArgs e)
        {
if (!Directory.Exists("E:\\baiju\\Test"))
                {
                    System.IO.Directory.CreateDirectory("E:\\baiju\\Test");
                }
                    using (var sw = new StreamWriter("E:\\baiju\\Test\\Test.csv"))
                    {
                         var writer = new CsvWriter(sw);
                        List<AuctionUpsert> list = new List<AuctionUpsert>();
                        IEnumerable records = list;
                        writer.WriteRecords(records);
                        writer.NextRecord();
                     }
}

While I am doing this Only the field names in the DataSet class are shown in the CSV file. I want the value of each fields in the CSV file.

NB:- And the associated_images and start_time(That has value "started" by default) fields are not available there also.
Thanks in advance.
Posted
Comments
Richard Deeming 28-May-15 13:03pm    
You haven't mentioned where the CsvWriter class comes from - it's not a built-in class.

Is it the class from this project[^]?
Baiju christadima 29-May-15 3:39am    
CsvWriter is an object of the DLL named "CsvHelper."
Sergey Alexandrovich Kryukov 28-May-15 14:44pm    
What is that supposed to mean, "convert class to file". This is like converting a knife into a piece of meat. :-)
—SA
Maciej Los 28-May-15 15:10pm    
:laugh:

1 solution

Baiju christadima wrote:

List<AuctionUpsert> list = new List<AuctionUpsert>(); // <- EMPTY!
IEnumerable records = list; // <- ALSO EMPTY!
writer.WriteRecords(records);

While I am doing this Only the field names in the DataSet class are shown in the CSV file. I want the value of each fields in the CSV file.

You've passed an empty list to the WriteRecords method. There are no values to write.

If you want to write some data to the CSV file, then you'll need to pass that data to the CsvWriter.
 
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