-
January 26th, 2011, 01:50 PM
#1
loading CSV-File in DataGridView via OleDbConnection - Error
Hello,
i have to write a homework for my university (deadline is tonight...) and this OleDbConnection is making me mad.
I want to load a .csv-File into a Dataset and then fill the DataGridView with the DataSet, but I dont get it to work! The Code is okay but there is always a runtime error when I try to load the File.
I am sorry, my error is in german, but I will try to translate it:
"System.Data.OleDb.OleDbException (0x80004005): 'C:\csvFilesFolder\test.csv' ist kein zulässiger Pfad. Stellen Sie sicher, dass der Pfad richtig eingegeben wurde und dass sie mit dem Server, auf dem sich die Datei befindet, verbunden sind."
--->
"System.Data.OleDb.OleDbException (0x80004005): 'C:\csvFilesFolder\test.csv' is no correct pathname. Please make sure if the path is correctly spelled and that you are connected to the server, on which the file is saved." (followed by 8 more error-rows)
I have a button that loads the path in a textbox and uploads the file into my dataset.
I do not use the variable "txtFileName" yet, for I want to make it work in a static way first - so dont be confused.
What am I doing wrong?? Please help me asap...
Here is my source code:
private: System::Void btn_load_Click(System::Object^ sender, System::EventArgs^ e) {
Stream^ loadStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "C:\\";
openFileDialog1->Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 1;
openFileDialog1->FileName = txtFileName->Text;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms:ialogResult::OK )
{
if ( (loadStream = openFileDialog1->OpenFile()) != nullptr )
{
txtFileName->Text = openFileDialog1->FileName;
Import();
loadStream->Close();
this->dgv_cursos->DataSource = this->ds->Tables;
this->Show();
}
}
}
private: void Import()
{
try
{
String^ selectCommand = "select * from test.csv";
this->connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\csvFilesFolder\\test.csv;Extended Properties=\'text;HDR=Yes;FMT=Delimited\'";
this->oledbConn = (gcnew System:ata::OleDb::OleDbConnection(this->connString));
this->oledbDA = (gcnew System:ata::OleDb::OleDbDataAdapter(selectCommand, oledbConn));
this->oledbConn->Open();
this->ds = (gcnew System:ata:ataSet());
this->oledbDA->Fill(ds);
}
catch (System::Exception ^exp)
{
MessageBox::Show(exp->ToString(), "Error");
return;
}
finally
{
this->oledbConn->Close();
}
}
-
January 26th, 2011, 03:58 PM
#2
Re: loading CSV-File in DataGridView via OleDbConnection - Error
This Forum is for
Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.
Your code snippet looks like managed C++. So try to ask in the correct forum...
Victor Nijegorodov
-
January 27th, 2011, 05:20 AM
#3
Re: loading CSV-File in DataGridView via OleDbConnection - Error
-
January 27th, 2011, 05:47 PM
#4
Re: loading CSV-File in DataGridView via OleDbConnection - Error
Your deadline has probably passed, but you may still be interested in the answer to your question...
When accessing CSV files that way, the Data Source must be the directory that contains the CSV file. Threfore your connection string needs to be changed to:
Code:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\csvFilesFolder;Extended Properties='text;HDR=Yes;FMT=Delimited'
BTW, you don't need to escape single quotes in a C++ string literal.
I made some other changes to your code while testing, but they're mostly style-related and shouldn't influence the essential result.
BTW2, I'm no database expert either and found the answer using a forum search in an almost 6 years old thread in the C# forum: http://www.codeguru.com/forum/showthread.php?t=349475.
Please use code tags when posting code.
Ah and... Welcome to CodeGuru!
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|