Hullo,
I have a quite simple piece of code that returns a syntax error in the query execution: Have you got any idea about what might be wrong? - public void SQLCommandWithParam(string name, byte[] image)
-
{
-
OleDbCommand sqlCommand1 = new OleDbCommand();
-
System.Data.OleDb.OleDbConnection myOleConnection = new System.Data.OleDb.OleDbConnection();
-
myOleConnection.ConnectionString=connectionString();
-
myOleConnection.Open();
-
sqlCommand1.Connection = myOleConnection;
-
try
-
{
-
if (sqlCommand1.Parameters.Count ==0 )
-
{
-
sqlCommand1.CommandText = "INSERT INTO Image (Name, Immagine) " +
-
"VALUES(@Name, @Picture);";
-
sqlCommand1.Parameters.Add("@Name",
-
System.Data.OleDb.OleDbType.VarChar, 200);
-
sqlCommand1.Parameters.Add("@Picture",
-
System.Data.OleDb.OleDbType.Binary);
-
}
-
sqlCommand1.Parameters["@Name"].Value = name;
-
sqlCommand1.Parameters["@Picture"].Value = image;
-
sqlCommand1.Connection = myOleConnection;
-
int iresult=sqlCommand1.ExecuteNonQuery();
-
MessageBox.Show(Convert.ToString(iresult));
-
}
-
catch(Exception ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
}
6 5275 TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
I immediately follow your suggestion in posting another try, this time with an ODBCDrvriver: here the error appears at connection time and reports a reference to an unassigned object. This is the new code: -
-
public void SQLCommandWithParam(string name, byte[] image)
-
{
-
OdbcConnection conn = new OdbcConnection();
-
conn.ConnectionString = @"Dsn=.\DataBase\BRP.mdb";
-
conn.Open();
-
string sqlQuery = "INSERT INTO Image ([Name], [Picture]) " +
-
"VALUES(@Name, @Picture);";
-
OdbcCommand dbcommand = new OdbcCommand(sqlQuery, conn);
-
dbcommand.Parameters.Add("@Name", OdbcType.VarChar,200).Value = name;
-
dbcommand.Parameters.Add("@Picture", OdbcType.VarBinary).Value = image;
-
-
try
-
{
-
int count = dbcommand.ExecuteNonQuery();
-
}
-
catch (OdbcException ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
}
-
reports a reference to an unassigned object
"How do I fix a 'object reference not set to an instance of an object' error?
The line your code stopped on while debugging holds all your answers.
One of the variables/objects was created but not initialized. For example: - string TempString;// Created but not initialized so this is still null
-
//versus
-
string TempString = string.empty;
Debug your project again. This time, when it breaks on a line look at the Locals pallet to see which variable/object is null. You can also hover your mouse over each variable and the hothelp will shows its value. One of them will be null.
So far I can get myself on my own feet..., after all I am in programming since my graduation 20 years ago, albeit not in the .Net environment and with gaps.
The problem is that the error is returned _inside_ the opening of the connection where the debugger has no way to enter.
In the previous case it was _inside_ the query execution, exactly in the same situation.
In the following the revised message: please delete the present one if possible, I am not able to do it myself.
In brief the only involved variable is conn, the connection, which has all values assigned but the Server Version and Base that cannot be otherwise because the connection is closed: what is not suprising given what is applied to the conn variable is the open operation. This is the expanded pallette.
Of course that might all boil down to the connection string I got from various internet sites: another one I found is the following, returning the same error: at the bottom the pallette for this other configuration:
Alternative connection string: -
OdbcConnection con = new OdbcConnection(@"{Microsoft Access Driver (*.mdb)};DSN=.\DataBase\BRP.mdb;Uid=Admin;Pwd=;");
-
First pallette: -
-
- conn {System.Data.Odbc.OdbcConnection} System.Data.Odbc.OdbcConnection
-
+ base {System.Data.Odbc.OdbcConnection} System.Data.Common.DbConnection {System.Data.Odbc.OdbcConnection}
-
ConnectionString "Dsn=.\\DataBase\\BRP.mdb" string
-
ConnectionTimeout 15 int
-
Database "" string
-
DataSource "" string
-
Driver "" string
-
- ServerVersion 'conn.ServerVersion' ha generato un'eccezione di tipo 'System.InvalidOperationException' string {System.InvalidOperationException}
-
- base {"Operazione non valida. La connessione è chiusa."} System.SystemException {System.InvalidOperationException}
-
- base {"Operazione non valida. La connessione è chiusa."} System.Exception {System.InvalidOperationException}
-
+ Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
-
HelpLink null string
-
+ InnerException null System.Exception
-
Message "Operazione non valida. La connessione è chiusa." string
-
Source "System.Data" string
-
StackTrace " in System.Data.ProviderBase.DbConnectionClosed.get_ServerVersion()\r\n in System.Data.Odbc.OdbcConnection.get_ServerVersion()" string
-
+ TargetSite {System.String get_ServerVersion()} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
-
+ Membri statici
-
+ Membri non pubblici
-
Second pallette: -
- con {System.Data.Odbc.OdbcConnection} System.Data.Odbc.OdbcConnection
-
- base {System.Data.Odbc.OdbcConnection} System.Data.Common.DbConnection {System.Data.Odbc.OdbcConnection}
-
+ base {System.Data.Odbc.OdbcConnection} System.ComponentModel.Component {System.Data.Odbc.OdbcConnection}
-
ConnectionString "{Microsoft Access Driver (*.mdb)};DSN=.\\DataBase\\BRP.mdb;Uid=Admin;Pwd=;" string
-
ConnectionTimeout 15 int
-
Database "" string
-
DataSource "" string
-
- ServerVersion '((System.Data.Common.DbConnection)(con)).ServerVersion' ha generato un'eccezione di tipo 'System.InvalidOperationException' string {System.InvalidOperationException}
-
+ base {"Operazione non valida. La connessione è chiusa."} System.SystemException {System.InvalidOperationException}
-
State Closed System.Data.ConnectionState
-
+ Membri non pubblici
-
ConnectionString "{Microsoft Access Driver (*.mdb)};DSN=.\\DataBase\\BRP.mdb;Uid=Admin;Pwd=;" string
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: kosta |
last post by:
hello! one of my forms communicates with a database, and is
supposed to add a row to a table using an Insert
statement... however, I get a 'oledb - syntax error'
exception... I have double...
|
by: Robert Hanson |
last post by:
Hi All,
I am trying to add a record to a datatable that is connected to an Access
database. I had no trouble with string and date fields, but for this
record, I have two Long Integer field...
|
by: Joe |
last post by:
Hello All,
I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message,
System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
...
|
by: Nathan Sokalski |
last post by:
When trying to submit data to an Access database using ASP.NET I recieve the
following error:
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
+41...
|
by: Joe |
last post by:
Hello All,
I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message,
System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
...
|
by: brianbasquille |
last post by:
Hello all,
Strange little problem here... am just trying to insert some basic
information into an Access Database using OleDB.
I'm getting a "Syntax error in Insert Into statement" when it...
|
by: Warex |
last post by:
Maybe someone can help, my code works untill it gets to update when it tells
me Syntax error in INSERT INTO statement.
Thought this was automaticaly created according to MS.
Im lost, my code is...
|
by: slinky |
last post by:
I'm getting a error when I open my . aspx in my browser... line 34:
da.Fill(ds, "Assets")
Here's the error and my entire code for this .aspx.vb is below
that ...
I need some clues as to what is...
|
by: gilesy |
last post by:
Hi, I have a ploblem with an insert statement using an access database, I have the same code with a sql database which works but it doesn't seem to work on access. Could someone please help.
For...
|
by: gilesy |
last post by:
Hi, I have a ploblem with an insert statement using an access database, I have the same code with a sql database which works but it doesn't seem to work on access. Could someone please help.
For i =...
|
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: 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |