1
Answer

Import CSV file to my Asp.net Website

Venkat Mungari

Venkat Mungari

12y
2.7k
1

i have a problem with .csv file importing to my website

public DataTable ReadDataFromCSVFormat()
        {
            ReadColumnsfromCSVfile(file,isRowOneHeader);
            CreateDataSet();
          
  return CSVData;//error comes in this place
        }

 public  DataTable ReadColumnsfromCSVfile(string file, bool isRowOneHeader)
      {

       DataTable csvDataTable = new DataTable();

    //no try/catch - add these in yourselfs or let exception happen
    //String[] csvData = File.ReadAllLines(HttpContext.Current.Server.MapPath(file));
      // String[] csvData =  Server.MapPath(this.UploadFolderPath) + FileUploadctrl.FileName;
       String[] csvData = File.ReadAllLines(HttpContext.Current.Server.MapPath(this.UploadFolderPath) + FileUploadctrl.FileName);
    //if no data in file 'manually' throw an exception
    if (csvData.Length == 0)
    {
        throw new Exception("CSV File Appears to be Empty");
    }

    String[] headings = csvData[0].Split(',');
    int index = 0; //will be zero or one depending on isRowOneHeader

    if (isRowOneHeader) //if first record lists headers
    {
        index = 1; //so we won't take headings as data

        //for each heading
        for (int i = 0; i < headings.Length; i++)
        {
            //replace spaces with underscores for column names
            headings[i] = headings[i].Replace(" ", "_");

            //add a column for each heading
            csvDataTable.Columns.Add(headings[i], typeof (string));
        }
    }
    else //if no headers just go for col1, col2 etc.
    {
        for (int i = 0; i < headings.Length; i++)
        {
            //create arbitary column names
            csvDataTable.Columns.Add("col" + (i + 1).ToString(), typeof (string));
        }
    }

    //populate the DataTable
    for (int i = index; i < csvData.Length; i++)
    {
        //create new rows
        DataRow row = csvDataTable.NewRow();

        for (int j = 0; j < headings.Length; j++)
        {
            //fill them
            row[j] = csvData[i].Split(',')[j];
        }

        //add rows to over DataTable
        csvDataTable.Rows.Add(row);
    }

        //return the CSV DataTable
       return csvDataTable;

     }
 please check it......







Answers (1)
0
Santiago Felipe
NA 2 0 9y
Do you mean you want to convert the JPEG image to PNG while loading the JPEG image (and not after)?

Are you selecting and loading the JPEG image using the OpenFileDialog Class?

If yes, you can get the name of the JPEG file, pass it Image.FromFile method:
https://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx
And then save it to PNG using the
Image.Save method (Stream, ImageFormat):
https://msdn.microsoft.com/en-us/library/ms142147.aspx

But if you face any problem while loading or saving the JPEG or PNG images, you can try using loading and saving features from leadtools programming toolkit.
0
Raja T
NA 7.4k 6k 9y
Hi,

Please refer below URLs

http://stackoverflow.com/questions/17294737/converting-an-image-to-png-on-upload


http://stackoverflow.com/questions/3906260/how-can-i-convert-a-jpeg-image-to-a-png-one-with-transparent-background


http://www.c-sharpcorner.com/UploadFile/scottlysle/ImageConverter09132006105604AM/ImageConverter.aspx

Thanks