tags:

views:

326

answers:

3

I am using VFPOLEDB driver to read DBF files and I keep getting this error and I am not sure why and how to fix the problem:

The provider could not determine the Decimal value. For example, the row was just created, the default for the Decimal column was not available, and the consumer had not yet set a new Decimal value.

Here is the code. I call this routine to return a DataSet of the DBF file and display the data in a DataGridView.

   public DataSet GetDBFData(FileInfo fi, string tbl)
    {
        using (OleDbConnection conn = new OleDbConnection(
        @"Provider=VFPOLEDB.1;Data Source=" + fi.DirectoryName + ";"))
        {
            conn.Open();
            string command = "SELECT * FROM " + tbl;
            OleDbDataAdapter da = new OleDbDataAdapter(command, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
    }
A: 

If you add a row from your gridview, it doesn't necessarily use a default value, but rather NULLs, so you may need to pre-set your defaults, or set the schema to NOT Allow Nulls.

You could automate through the columns after the query is done and force defaults based on the columns data types, such as

foreach (DataColumn oDC in YourDataSet.Tables[0].Columns)
{
   if (oDC.DataType.ToString().Contains("String"))
    oDC.DefaultValue = "";
   else if (oDC.DataType.ToString().Contains("Int32"))
    oDC.DefaultValue = 0;
   else if (oDC.DataType.ToString().Contains("DateTime"))
    oDC.DefaultValue = DateTime.MinValue;
}

these are just 3 default types, but there could be others like boolean, decimal, float, whatever, just add into the if/else and put whatever "default" values. It MAY help where otherwise "NULL" values are getting injected in when adding new rows.

DRapp
How do I preset my defaults? I am trying to preview the contents of a arbitrary DBF file.
John Sheares
Thanks for the suggestion, but I can't even get a DataSet and my code crashes at da.Fill(ds);
John Sheares
see my code sample in another post at http://stackoverflow.com/questions/2184490/search-in-dbf-file-using-idx-file/2184598#2184598 subject title was Search in DBF file using .idx file
DRapp
I tried using your code, but it still has the same problem. I have identified the offending column in one of the DBFs files. The column contains doubles with alot of decimal places.
John Sheares
A: 

I finally solved the problem by getting the table schema and then casting all of non-character fields to varchar in the select statement. Good enough for previewing the contents of the table.

John Sheares
A: 

I found the solution here:

http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/035b6676-441f-47df-9272-3b2bc4f8861a

Select Cast(FieldName As NUMERIC(11,3)) From TableName
John Prado