473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Opening a file in visual c# not a valid filename

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
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click ( object sender , EventArgs e )
  2.             {
  3.             System.Windows.Forms.DataGrid ZipCode=null; 
  4.             System . IO . Stream myStream = null;
  5.             OpenFileDialog openFileDialog1 = new OpenFileDialog ( );
  6.             openFileDialog1 . InitialDirectory = "c:\\";
  7.             openFileDialog1 . Filter = "dbf files (*.dbf)|*.dbf";
  8.             openFileDialog1 . FilterIndex = 0;
  9.             openFileDialog1 . RestoreDirectory = true;
  10.             openFileDialog1 . CheckFileExists = true;
  11.             openFileDialog1 . CheckPathExists = true;
  12.             if ( openFileDialog1 . ShowDialog ( ) == DialogResult . OK )
  13.                 {
  14.                 try
  15.                     {
  16.                     if ( ( myStream = openFileDialog1 . OpenFile ( ) ) != null )
  17.                         {//if
  18.                         using ( myStream )
  19.                             {//using
  20.                         string fullPathname = openFileDialog1 . FileName;
  21.                         string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\;Extended Properties=dBASE IV;User ID=Admin;Password=";
  22.                         string commandString = "Select * from '" + fullPathname + "'";
  23.                         OleDbDataAdapter DataAdapter = new OleDbDataAdapter (commandString,connectionString );
  24.                         DataSet DataSet = new DataSet ( );
  25.                         DataAdapter . Fill ( DataSet , fullPathname );
  26.               ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
  27.                             } //end using
  28.                         } //end if                    
  29.                  }catch( Exception ex )
  30.                     {
  31.                     MessageBox . Show ( "  error: " + ex . Message );
  32.                     }
  33.                 }
  34.             }
  35.  
Nov 3 '07 #1
6 5721
balabaster
797 Expert 512MB
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...
Nov 3 '07 #2
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
Expand|Select|Wrap|Line Numbers
  1. 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
Nov 4 '07 #3
balabaster
797 Expert 512MB
the fille exists it is a .dbf file from the usps. I have been debugging and the line that triggers the exception is
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. Dim fileName As String = "C:\MyDB.accdb" 
  2. Dim tableName As String = "MyTable"
  3. Dim sConStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileName & ";Persist Security Info=False;"
  4. Dim oDA As New OleDbDataAdapter("Select * From " & tableName, sConStr)
  5. Dim oDS As New DataSet("MyDataSet")
  6. oDA.Fill(oDS, "MyTable")
  7. ListBox1.DataSource = oDS.Tables("MyTable")
  8.  
C#
Expand|Select|Wrap|Line Numbers
  1. string fileName = "C:\\MyDB.accdb"; 
  2. string tableName = "MyTable";
  3. string sConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Persist Security Info=False;
  4. OleDbDataAdapter oDA = New OleDbDataAdapter("Select * From " + tableName, sConStr);
  5. DataSet oDS = New DataSet("MyDataSet");
  6. oDA.Fill(oDS, "MyTable");
  7. ListBox1.DataSource = oDS.Tables["MyTable"];
  8.  
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.
Nov 4 '07 #4
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
Expand|Select|Wrap|Line Numbers
  1. try
  2.          {
  3.          string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\ZIP.DBF;Extended Properties=dBase IV";
  4.          OleDbConnection conn = new OleDbConnection ( conString );
  5.          command = conn . CreateCommand ( );
  6.  
  7.          // create the DataSet
  8.          DataSet ds = new DataSet ( );
  9.          dataGridView1 . DataSource = null;
  10.  
  11.          // open the connection
  12.          conn . Open ( );
  13.          string commandString = "Select * from  ZIP.DBF";
  14.          // run the query
  15.          command . CommandText = commandString;
  16.          OleDbDataAdapter adapter = new OleDbDataAdapter ( command );
  17.          adapter . Fill ( ds );
  18.  
  19.          // close the connection
  20.          conn . Close ( );
  21.  
  22.          // set the grid's data source
  23.          dataGridView1 . DataSource = ds . Tables [ 0 ];
  24.          }
  25.      catch ( Exception ex)
  26.          {
  27.          MessageBox . Show (  ex . Message );
  28.  
  29.          }             
  30.  
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
Nov 5 '07 #5
I have cured this problem with this line
Expand|Select|Wrap|Line Numbers
  1. fullPath2 = Path . GetDirectoryName ( openFileDialog1 . FileName );
then I feed fullpath2 into my connection string and It worked. thanks for all of your help
John Larson
Nov 7 '07 #6
Plater
7,872 Expert 4TB
I have cured this problem with this line
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 7 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
14
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...
0
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...
13
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...
9
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...
11
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...
4
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....
2
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
1
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)...
0
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...
1
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....
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.