none
opening dbf files with open file dialog and going nuts

    Question

  • I am trying to open and query a dbf file using OpenFileDialog. MY error is "could not read file from disk with c:\MYFILENAME.DBF not a valid file name" what does a valid file name consist of.
    thank you John Larson

    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 = 2;
                openFileDialog1 . RestoreDirectory = true;

                if ( openFileDialog1 . ShowDialog ( ) == DialogResult . OK )
                    {
                    try
                        {
                        if ( ( myStream = openFileDialog1 . OpenFile ( ) ) != null )
                            {//if
                            using ( myStream )
                                {//using
                            string fullPathname = openFileDialog1 . FileName;
                                // Insert code to read the stream here.
                            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: Could not read file from disk. Original error: " + ex . Message );
                        }
                    }
                }

    Saturday, November 3, 2007 3:28 AM

All replies

  • I actually do not see that you concatenate selected file name to your connection string and trying to open connection to the root folder on C: drive. I believe your connection should look like

     

    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullPathname + ";User ID=Admin;Password=";Extended Properties=\"dBASE IV\";"
    Monday, November 5, 2007 11:23 AM
    Moderator
  • There are too many characters in the filename. The dBase ISAM driver limit is eight characters (not counting the file extension). 

     

    Monday, November 5, 2007 1:52 PM
  • thanks for all of your input. I used

    this code and I am able to open the file and display it. like a lot of  problems with file i/o it turned out to be an path problem

    Code Block
    fullpath2=Path.GetDirectoryName(openFileDialog1.FileName);

     

     

    now when I try and display the results of a query based on variables from a combo box and a text

    box it comes up blank but has the correct field  names at the top of the datagridview but no data

    Code Block

    private void button2_Click ( object sender , EventArgs e )

    {

    string field = comboBox1 . Text;

    string query = textBox1 . Text;

    try

    {

    fullPath2 = Path . GetDirectoryName ( openFileDialog1 . FileName );

    conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + fullPath2 + "';Extended Properties=dBase IV";

    conn = new OleDbConnection ( conString );

    command = conn . CreateCommand ( );

    dataGridView1 . DataSource = null;

    // create the DataSet

    DataSet ds = new DataSet ( );

    // open the connection

    conn . Open ( );

    // run the query

    string commandString = "Select * from ZIP.DBF where '" + field + "'='"+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 );

    }

    }

     

    Wednesday, November 7, 2007 8:00 PM
  • If no data is being returned then it's probably because there isn't any which satisfies the criteria in your WHERE statement. Try removing the WHERE statement first to see if any data is returned.

    Thursday, November 8, 2007 2:11 PM
  • the record exists. i am querying a zip code file from the govt. removing the where clause returns data

    when I put a watch on the commandString the select statement seems right. How do I do a where clause with variables

    Saturday, November 10, 2007 2:58 AM
  • Hey,

    In your command string it should be something like this:

    Code Block

    // run the query;


    string tableName = "SomeTableName";

    string commandString = "Select * from " + tableName + " where " + field + " ='"+query+"'"


    You cannot have the database file name, but the name of the table from which you would want to retrieve the data.

    I hope "field" refers to the name of the column in "tableName" table, hence it should not be enclosed in single quotes. Since "query" is the value of the column should be enclosed in single quotes.

    It would be good if you first test the above statement with some known values making sure that they return a known number of rows.

    Add this line of code which might help you:


    Code Block

    // set the grid's data source


    dataGridView1.DataMember = ds.DefaultViewManager;

    dataGridView1.DataSource = ds.Tables[0].TableName;


    Hope this helps,

    Regards,
    Abhisheik.

    Monday, November 12, 2007 6:47 AM