c#


Recieving an error when insert data into a .dbf file


Back gournd
I am currently working on a project which will need me to create a local .dbf file which I must then populate with a value. I am currently able to create a .dbf file in a test dir and add a column to it, however when I later try to add a value to the column it errors out.
Problem
I am currently not able to write to the column Public in the dbf file which I create. When it goes into ExecuteNonQuery(); it errors out and is caught in my catch statement.
Working Code
public static bool CreateDBF()
{
try
{
string dbfDirectory = #"c:\Users\me\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
InsertDataIntoDBF(dbfDirectory + "\\CustomProperties.DBF");
}
return true;
}
catch (Exception ex)
{
throw;
}
}
Working Code - Appendix
The above block of code successfully create the dbf file with the column which I want as the following image will show
Not Working Code
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = #"INSERT INTO CustomProperties (Public)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("#Public", "True");
connection.Open();
new OleDbCommand("set null off", connection).ExecuteNonQuery();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
throw;
}
}
Updated Code Blocks
Working Block
public static bool CreateDBF()
{
try
{
string dbfDirectory = #"c:\Users\er4505\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\\CustomProperties.DBF");
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false; << I have a breakpoint here
}
}
Non-Working
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = #"INSERT INTO CustomProperties (Public) VALUES (#Public)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("#Public", "True");
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false; << breakpoint here
}
}
Error Caught
{"oledbErrors":[{"Message":"Syntax error.","NativeError":0,"Source":"Microsoft OLE DB Provider for Visual FoxPro","SQLState":""}],"ClassName":"System.Data.OleDb.OleDbException","Message":"Syntax error.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)\r\n at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)\r\n at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)\r\n at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)\r\n at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)\r\n at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()\r\n at CasewareIntegrationWebApi.Services.DatabaseServices.InsertDataIntoDBF(String path) in C:\\Users\\er4505\\Desktop\\Engagement Retival Util\\CasewareIntegrationWebApi\\CasewareIntegrationWebApi\\Services\\DatabaseServices.cs:line 88","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nExecuteCommandTextErrorHandling\nSystem.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Data.OleDb.OleDbCommand\nVoid ExecuteCommandTextErrorHandling(System.Data.OleDb.OleDbHResult)","HResult":-2147217900,"Source":"Microsoft OLE DB Provider for Visual FoxPro","WatsonBuckets":null}
This works for me. Try it
static void Main(string[] args)
{
try
{
CreateDBF();
Console.WriteLine(ReadDB());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static string dbfDirectory = #"c:\Test\dbTest";
private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbfDirectory + ";Extended Properties = dBase IV";
public static bool CreateDBF()
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "Create Table CustomProperties ([Public] char(50))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\\CustomProperties.DBF");
return true;
}
public static bool InsertDataIntoDBF(string path)
{
string query = #"INSERT INTO CustomProperties ([Public]) VALUES (#Public)";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("#Public", "True");
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
public static string ReadDB()
{
string res = string.Empty;
string query = #"SELECT * FROM CustomProperties";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("#Public", "True");
connection.Open();
res = (string)command.ExecuteScalar();
connection.Close();
}
return res;
}
The error was purely syntax, and I was guided by a really good example which I found in the following link
https://social.msdn.microsoft.com/Forums/en-US/24eac4c5-3a4d-43f4-8607-ef684919c4af/command-contains-unrecognized-phrasekeyword-vbnet?forum=visualfoxprogeneral
Working Code Blocks
public static bool CreateDBF()
{
try
{
string dbfDirectory = #"c:\Users\er4505\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\\CustomProperties.DBF");
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false;
}
}
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = "INSERT INTO CustomProperties(Public) VALUES (?)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
connection.Open();
OleDbCommand cmdInit = new OleDbCommand("set null off", connection);
cmdInit.ExecuteNonQuery();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbParameter publicStatus = command.Parameters.Add("Public", OleDbType.Char);
publicStatus.Value = "True";
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
log4net.LogManager.GetLogger("EmailLogger").Error(JsonConvert.SerializeObject(ex));
string viewError = JsonConvert.SerializeObject(ex);
return false;
}
}
public static bool CreateDBF()
{
...
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
...
}
InsertDataIntoDBF(dbfDirectory + "\\CustomProperties.DBF"); // change 1
return true;
}
catch (Exception)
{
throw; //change2
}
}
public static bool InsertDataIntoDBF(string path)
{
try
{
...
}
catch (Exception)
{
throw; //change 3
}
}

Related Links

Azure Mobile Service - Custom API runs locally but get 404 when Deployed to Azure
Rewriting a ? operator with an if-else in c#
Which custom dictionary rule handles an acronym with a number?
Spreadsheetgear in parallel [closed]
WPF Cross-Thread MessageBox not blocking main thread input
HttpRequestMessage configure IP in multi-IP environment
WindowsAPICodePack - Is System.KindText value consistent regardless of Globalized Culture?
Lookup and replace multiple value pairs
get group and role details from AAD in a native application
Template binding of dependency property inside generic.Xaml
How to get the content of an Image inside a Rectangle?
Separate GridView column in two with condition
How can I open popups in the same WebView (Not a New Window) in Windows UWP?
WinForm looks different than in VS designer [duplicate]
Installing Windows 8.1 sideloaded app without powershell command
Slider completely change ticks more than once

Categories

HOME
indexing
optimization
cors
cluster-analysis
wifi
display
android-ndk
postsharp
where
ms-access-2010
angularjs-directive
powerquery
opacity
collision-detection
ejb-3.0
suitecrm
ng2-charts
xmlhttprequest
oxid
infopath
combinations
javaagents
beaker-notebook
tcpdf
uipath
resx
launchd
ocl
keil
oracle-nosql
favorites
traitsui
angularjs-resource
area
motion
byobu
icu
delphi-xe
definition
qt-quick
npoco
spring-webflow-2
jks
import.io
dmalloc
critical-section
robotc
huawei
qudpsocket
optional-parameters
racket-student-languages
3d-rendering
radar-chart
jersey-test-framework
apache2-module
google-finance-api
overlapping
skscene
transbase
page-flipping
textblob
avrcp
web-standards
copy-on-write
wingdb
pymol
dojox.grid
nsurlrequest
tigase
indic
disabled-control
reactjs-native
enquire.js
rssi
mixradio
nvcc
db2-connect
mdp
vbaccelerator
misfire-instruction
aspnet-compiler
static-class
ojb
violation
source-depot
pyxplot
exi
auctex
system.transactions
cron4j
feincms
mail-gem
netzke
helicontech
umfpack
activescaffold
uccapi
hp-trim
unauthorized
httpbrowsercapabilities
asp.net-1.1
tracd
memory-consumption

Resources

Encrypt Message