Object reference not set to and instance of an object
string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
I really dont understand what problem in this line, Anyone can help me, pls
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string fileCSV; //full file name
private string dirCSV; //directory of file to import
private string fileNevCSV; //name (with extension) of file to import - field
public string FileNevCSV //name (with extension) of file to import - property
{
get { return fileNevCSV; }
set { fileNevCSV = value; }
}
private string strFormat; //CSV separator character
private string strEncoding; //Encoding of CSV file
private long rowCount = 0; //row number of source file
private void writeSchema()
{
try
{
FileStream fsOutput = new FileStream(this.dirCSV + "\\schema.ini", FileMode.Create, FileAccess.Write);
StreamWriter srOutput = new StreamWriter(fsOutput);
string s1, s2, s3, s4, s5;
s1 = "[" + this.FileNevCSV + "]";
s2 = "ColNameHeader=" + chkFirstRowColumnNames.Checked.ToString();
s3 = "Format=" + this.strFormat;
s4 = "MaxScanRows=25";
s5 = "CharacterSet=" + this.strEncoding;
srOutput.WriteLine(s1.ToString() + "\r\n" + s2.ToString() + "\r\n" + s3.ToString() + "\r\n" + s4.ToString() + "\r\n" + s5.ToString());
srOutput.Close();
fsOutput.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "writeSchema");
}
finally
{ }
}
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
}
}
private void btnPreview_Click(object sender, EventArgs e)
{
loadPreview();
}
private void loadPreview()
{
try
{
// select format, encoding, an write the schema file
//Format();
//Encoding();
//writeSchema();
// loads the first 500 rows from CSV file, and fills the
// DataGridView control.
this.dataGridView_preView.DataSource = LoadCSV(500);
this.dataGridView_preView.DataMember = "csv";
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error - loadPreview", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public DataSet LoadCSV(int numberOfRows)
{
DataSet ds = new DataSet();
try
{
// Creates and opens an ODBC connection
string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
string sql_select;
OdbcConnection conn;
conn = new OdbcConnection(strConnString.Trim());
conn.Open();
//Creates the select command text
if (numberOfRows == -1)
{
sql_select = "select * from [" + this.FileNevCSV.Trim() + "]";
}
else
{
sql_select = "select top " + numberOfRows + " * from [" + this.FileNevCSV.Trim() + "]";
}
//Creates the data adapter
OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);
//Fills dataset with the records from CSV file
obj_oledb_da.Fill(ds, "csv");
//closes the connection
conn.Close();
}
catch (Exception e) //Error
{
MessageBox.Show(e.Message, "Error - LoadCSV", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return ds;
}
private void tbFile_TextChanged(object sender, EventArgs e)
{
// full file name
this.fileCSV = this.txtFileToImport.Text;
// creates a System.IO.FileInfo object to retrive information from selected file.
System.IO.FileInfo fi = new System.IO.FileInfo(this.fileCSV);
// retrives directory
this.dirCSV = fi.DirectoryName.ToString();
// retrives file name with extension
this.FileNevCSV = fi.Name.ToString();
// database table name
}
private void chkFirstRowColumnNames_CheckedChanged(object sender, EventArgs e)
{
}
}
}