I am using an openfiledialoge to open a dbf file in a windows app. the error I get is c:\myfile.dbf not an valid filename. here is my code -
private void button1_Click ( object sender , EventArgs e )
-
{
-
System.Windows.Forms.DataGrid ZipCode=null;
-
System . IO . Stream myStream = null;
-
OpenFileDialog openFileDialog1 = new OpenFileDialog ( );
-
openFileDialog1 . InitialDirectory = "c:\\";
-
openFileDialog1 . Filter = "dbf files (*.dbf)|*.dbf";
-
openFileDialog1 . FilterIndex = 0;
-
openFileDialog1 . RestoreDirectory = true;
-
openFileDialog1 . CheckFileExists = true;
-
openFileDialog1 . CheckPathExists = true;
-
if ( openFileDialog1 . ShowDialog ( ) == DialogResult . OK )
-
{
-
try
-
{
-
if ( ( myStream = openFileDialog1 . OpenFile ( ) ) != null )
-
{//if
-
using ( myStream )
-
{//using
-
string fullPathname = openFileDialog1 . FileName;
-
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\;Extended Properties=dBASE IV;User ID=Admin;Password=";
-
string commandString = "Select * from '" + fullPathname + "'";
-
OleDbDataAdapter DataAdapter = new OleDbDataAdapter (commandString,connectionString );
-
DataSet DataSet = new DataSet ( );
-
DataAdapter . Fill ( DataSet , fullPathname );
-
ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
-
} //end using
-
} //end if
-
}catch( Exception ex )
-
{
-
MessageBox . Show ( " error: " + ex . Message );
-
}
-
}
-
}
-
6 5721
I am using an openfiledialoge to open a dbf file in a windows app. the error I get is c:\myfile.dbf not an valid filename. here is my code
<code>
private void button1_Click ( object sender , EventArgs e )
{
System.Windows.Forms.DataGrid ZipCode=null;
System . IO . Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog ( );
openFileDialog1 . InitialDirectory = "c:\\";
openFileDialog1 . Filter = "dbf files (*.dbf)|*.dbf";
openFileDialog1 . FilterIndex = 0;
openFileDialog1 . RestoreDirectory = true;
openFileDialog1 . CheckFileExists = true;
openFileDialog1 . CheckPathExists = true;
if ( openFileDialog1 . ShowDialog ( ) == DialogResult . OK )
{
try
{
if ( ( myStream = openFileDialog1 . OpenFile ( ) ) != null )
{//if
using ( myStream )
{//using
string fullPathname = openFileDialog1 . FileName;
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\;Extended Properties=dBASE IV;User ID=Admin;Password=";
string commandString = "Select * from '" + fullPathname + "'";
OleDbDataAdapter DataAdapter = new OleDbDataAdapter (commandString,connectionString );
DataSet DataSet = new DataSet ( );
DataAdapter . Fill ( DataSet , fullPathname );
ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
} //end using
} //end if
}catch( Exception ex )
{
MessageBox . Show ( " error: " + ex . Message );
}
}
}
</code>
Well my first thought is that the file doesn't exist...
Well my first thought is that the file doesn't exist...
the fille exists it is a .dbf file from the usps. I have been debugging and the line that triggers the exception is -
ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
when I put a watch on the line it says the DataSet.Tables[fullPathname] is null
thanks for your time
John Larson
the fille exists it is a .dbf file from the usps. I have been debugging and the line that triggers the exception is -
ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
when I put a watch on the line it says the DataSet.Tables[fullPathname] is null
thanks for your time
John Larson
Does your database have a table in it with the same name as your DBF? I've gotta say I'm clueless on DBase so I'm running blind - I only have MS Access and SQL Server and my example works...
VB - Dim fileName As String = "C:\MyDB.accdb"
-
Dim tableName As String = "MyTable"
-
Dim sConStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileName & ";Persist Security Info=False;"
-
Dim oDA As New OleDbDataAdapter("Select * From " & tableName, sConStr)
-
Dim oDS As New DataSet("MyDataSet")
-
oDA.Fill(oDS, "MyTable")
-
ListBox1.DataSource = oDS.Tables("MyTable")
-
C# - string fileName = "C:\\MyDB.accdb";
-
string tableName = "MyTable";
-
string sConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Persist Security Info=False;
-
OleDbDataAdapter oDA = New OleDbDataAdapter("Select * From " + tableName, sConStr);
-
DataSet oDS = New DataSet("MyDataSet");
-
oDA.Fill(oDS, "MyTable");
-
ListBox1.DataSource = oDS.Tables["MyTable"];
-
So given what you've said, I'm curious if the structure of your database is as you expect...or that you've overlooked something in that area.
I am using the same file path as you are and my application cannot find the file. I have changed a lot of code this is what I am trying to make work -
try
-
{
-
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\ZIP.DBF;Extended Properties=dBase IV";
-
OleDbConnection conn = new OleDbConnection ( conString );
-
command = conn . CreateCommand ( );
-
-
// create the DataSet
-
DataSet ds = new DataSet ( );
-
dataGridView1 . DataSource = null;
-
-
// open the connection
-
conn . Open ( );
-
string commandString = "Select * from ZIP.DBF";
-
// run the query
-
command . CommandText = commandString;
-
OleDbDataAdapter adapter = new OleDbDataAdapter ( command );
-
adapter . Fill ( ds );
-
-
// close the connection
-
conn . Close ( );
-
-
// set the grid's data source
-
dataGridView1 . DataSource = ds . Tables [ 0 ];
-
}
-
catch ( Exception ex)
-
{
-
MessageBox . Show ( ex . Message );
-
-
}
-
I have also downloaded a shareware dbf viewer and it opens the file and correctly displays the file columns and rows
john larson
thanks for the time
I have cured this problem with this line -
fullPath2 = Path . GetDirectoryName ( openFileDialog1 . FileName );
then I feed fullpath2 into my connection string and It worked. thanks for all of your help
John Larson
I have cured this problem with this line -
fullPath2 = Path . GetDirectoryName ( openFileDialog1 . FileName );
then I feed fullpath2 into my connection string and It worked. thanks for all of your help
John Larson
I was going to say your connection string does not point to a database, just a path.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Vinay |
last post by:
Hi
I have a corrupt word file. I am able to open it with the code given below
tr
Dim pInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo(
pInfo.UseShellExecute...
|
by: D. Alvarado |
last post by:
Hello,
I am trying to open a window containing an image and I would like
the image to be flush against the window -- i.e. have no padding or
border. Can I make this happen with a single call to a...
|
by: Sarah |
last post by:
Hi,
I'm opening a MS Word file through c#
// Open the specified document
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
FileInfo fi = di.GetFiles("myTest.doc");
if...
|
by: Sky Sigal |
last post by:
I have created an IHttpHandler that waits for uploads as attachments for a
webmail interface, and saves it to a directory that is defined in
config.xml.
My question is the following:
assuming...
|
by: Rocky |
last post by:
I have 2 textboxes. When I click submit, i want to add whatevers in the text
box1 as username and whatevers in textbox2 as userid into an xml file. How do
I do that in ASP.NET using vb.net?
My...
|
by: aldrin |
last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0
compiler with great difficulty.There is no problem with running this
under KDevelop in linux. Any help would be greatly...
|
by: Charlie Brookhart |
last post by:
I am trying to write a code for a button click event. When the button is
clicked, it is supposed to bring up an open file dialog box to allow the
user to select the document they which to open....
|
by: Craig |
last post by:
Hi there,
I'm trying to open colour BMPs using PIL and I'm getting the following
errors.
Opening a 16 colour BMP I get:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |