openFileDialog error

  • Thread starter Thread starter Kylee Fenwick
  • Start date Start date
K

Kylee Fenwick

Guest
I have this code, but when I attempt to load the file, it says there is an error Please select a file even though a csv has been selected.

What am I am doing wrong?


using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace CSVImporter
{
public partial class CSVImporter : Form
{
public CSVImporter()
{
InitializeComponent();
}

// btBrowse
private void btBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// To list only csv files, we need to add this filter
openFileDialog.Filter = "|*.csv";
DialogResult result = openFileDialog.ShowDialog();

if (result == DialogResult.OK)
{
tbCSVPath.Text = openFileDialog.FileName;
}
}

// btUpload
private void btUpload_Click(object sender, EventArgs e)
{
try
{
dgCSVData.DataSource = GetDataTableFromCSVFile(tbCSVPath.Text);

foreach (DataGridViewColumn column in dgCSVData.Columns)
{
if (!column.Name.Equals("Current Count"))
column.ReadOnly = true;
}
}
catch (Exception ex)
{
MessageBox.Show("Please select a file", "My error message", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

// Read data from CSV
private static DataTable GetDataTableFromCSVFile(string csvfilePath)
{
DataTable csvData = new DataTable();
using (TextFieldParser csvReader = new TextFieldParser(csvfilePath))
{
csvReader.SetDelimiters(new string[] { ";" });
csvReader.HasFieldsEnclosedInQuotes = true;

//Read columns from CSV file, remove this line if columns not exits
string[] colFields = csvReader.ReadFields();

foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}

while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData == "")
{
fieldData = null;
}
}
csvData.Rows.Add(fieldData);

}
// new code to prevent Item Description column from sorting
csvData.DefaultView.Sort = "Item Description";
}
return csvData;
}

// btSave
private void btSave_Click(object sender, System.EventArgs e)
{
// Displays a SaveFileDialog so the user can save the file
// assigned to btSaveCSV.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "|*.csv";
saveFileDialog1.Title = "Save CSV File";
saveFileDialog1.ShowDialog();

// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{

// Calls write WriteDataTable on buttonClick using StreamWriter
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
{
WriteDataTable(dgCSVData.DataSource as DataTable, sw, true);
}
}
}

// Write data to csv
public static void WriteDataTable(DataTable sourceTable, TextWriter writer, bool includeHeaders)
{
if (includeHeaders) {
List<string> headerValues = new List<string>();
foreach (DataColumn column in sourceTable.Columns) {
headerValues.Add(QuoteValue(column.ColumnName));
}

writer.WriteLine(String.Join(",", headerValues.ToArray()));
}

string[] items = null;
foreach (DataRow row in sourceTable.Rows) {
items = row.ItemArray.Select(o => QuoteValue(o.ToString())).ToArray();
writer.WriteLine(String.Join(",", items));
}

writer.Flush();
}

private static string QuoteValue(string value)
{
return String.Concat("\"", value.Replace("\"", "\"\""), "\"");
}

}
}

Continue reading...
 
Back
Top