473,383 Members | 1,879 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,383 software developers and data experts.

Syntax error in OLEDB Insert statement with parameters

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?

Expand|Select|Wrap|Line Numbers
  1.          public void SQLCommandWithParam(string name, byte[] image)
  2.             {
  3.             OleDbCommand sqlCommand1 = new OleDbCommand();
  4.             System.Data.OleDb.OleDbConnection myOleConnection = new System.Data.OleDb.OleDbConnection();
  5.             myOleConnection.ConnectionString=connectionString();
  6.             myOleConnection.Open();
  7.             sqlCommand1.Connection = myOleConnection;
  8.             try
  9.                 {
  10.                 if (sqlCommand1.Parameters.Count ==0 )
  11.                     {
  12.                     sqlCommand1.CommandText = "INSERT INTO Image (Name, Immagine) " + 
  13.                            "VALUES(@Name, @Picture);";
  14.                     sqlCommand1.Parameters.Add("@Name",
  15.                              System.Data.OleDb.OleDbType.VarChar, 200);
  16.                     sqlCommand1.Parameters.Add("@Picture",
  17.                              System.Data.OleDb.OleDbType.Binary);
  18.                     }
  19.                 sqlCommand1.Parameters["@Name"].Value = name;
  20.                 sqlCommand1.Parameters["@Picture"].Value = image;
  21.                 sqlCommand1.Connection = myOleConnection;
  22.                 int iresult=sqlCommand1.ExecuteNonQuery();
  23.                 MessageBox.Show(Convert.ToString(iresult));
  24.                 }
  25.             catch(Exception ex)
  26.             {
  27.             MessageBox.Show(ex.Message);
  28.             }
  29.  }
Mar 23 '10 #1
6 5275
tlhintoq
3,525 Expert 2GB
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.
Mar 23 '10 #2
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void SQLCommandWithParam(string name, byte[] image)
  3.          {
  4.             OdbcConnection conn = new OdbcConnection();
  5.             conn.ConnectionString = @"Dsn=.\DataBase\BRP.mdb";
  6.             conn.Open();
  7.             string sqlQuery = "INSERT INTO Image ([Name], [Picture]) " +
  8.                            "VALUES(@Name, @Picture);";
  9.             OdbcCommand dbcommand = new OdbcCommand(sqlQuery, conn);
  10.             dbcommand.Parameters.Add("@Name", OdbcType.VarChar,200).Value = name;
  11.             dbcommand.Parameters.Add("@Picture", OdbcType.VarBinary).Value = image;
  12.  
  13.             try
  14.             {
  15.                 int count = dbcommand.ExecuteNonQuery();
  16.             }
  17.             catch (OdbcException ex)
  18.             {
  19.                 MessageBox.Show(ex.Message);
  20.             }
  21.           }
  22.  
Mar 23 '10 #3
tlhintoq
3,525 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. string TempString;// Created but not initialized so this is still null
  2. //versus
  3. 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.
Mar 23 '10 #4
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.
Mar 23 '10 #5
In the following the revised message: please delete the present one if possible, I am not able to do it myself.
Mar 23 '10 #6
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:
Expand|Select|Wrap|Line Numbers
  1.             OdbcConnection con = new OdbcConnection(@"{Microsoft Access Driver (*.mdb)};DSN=.\DataBase\BRP.mdb;Uid=Admin;Pwd=;");
  2.  
First pallette:

Expand|Select|Wrap|Line Numbers
  1.  
  2. -        conn    {System.Data.Odbc.OdbcConnection}    System.Data.Odbc.OdbcConnection
  3. +        base    {System.Data.Odbc.OdbcConnection}    System.Data.Common.DbConnection {System.Data.Odbc.OdbcConnection}
  4.         ConnectionString    "Dsn=.\\DataBase\\BRP.mdb"    string
  5.         ConnectionTimeout    15    int
  6.         Database    ""    string
  7.         DataSource    ""    string
  8.         Driver    ""    string
  9. -        ServerVersion    'conn.ServerVersion' ha generato un'eccezione di tipo 'System.InvalidOperationException'    string {System.InvalidOperationException}
  10. -        base    {"Operazione non valida. La connessione è chiusa."}    System.SystemException {System.InvalidOperationException}
  11. -        base    {"Operazione non valida. La connessione è chiusa."}    System.Exception {System.InvalidOperationException}
  12. +        Data    {System.Collections.ListDictionaryInternal}    System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
  13.         HelpLink    null    string
  14. +        InnerException    null    System.Exception
  15.         Message    "Operazione non valida. La connessione è chiusa."    string
  16.         Source    "System.Data"    string
  17.         StackTrace    "   in System.Data.ProviderBase.DbConnectionClosed.get_ServerVersion()\r\n   in System.Data.Odbc.OdbcConnection.get_ServerVersion()"    string
  18. +        TargetSite    {System.String get_ServerVersion()}    System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
  19. +        Membri statici        
  20. +        Membri non pubblici        
  21.  
Second pallette:

Expand|Select|Wrap|Line Numbers
  1. -        con    {System.Data.Odbc.OdbcConnection}    System.Data.Odbc.OdbcConnection
  2. -        base    {System.Data.Odbc.OdbcConnection}    System.Data.Common.DbConnection {System.Data.Odbc.OdbcConnection}
  3. +        base    {System.Data.Odbc.OdbcConnection}    System.ComponentModel.Component {System.Data.Odbc.OdbcConnection}
  4.         ConnectionString    "{Microsoft Access Driver (*.mdb)};DSN=.\\DataBase\\BRP.mdb;Uid=Admin;Pwd=;"    string
  5.         ConnectionTimeout    15    int
  6.         Database    ""    string
  7.         DataSource    ""    string
  8. -        ServerVersion    '((System.Data.Common.DbConnection)(con)).ServerVersion' ha generato un'eccezione di tipo 'System.InvalidOperationException'    string {System.InvalidOperationException}
  9. +        base    {"Operazione non valida. La connessione è chiusa."}    System.SystemException {System.InvalidOperationException}
  10.         State    Closed    System.Data.ConnectionState
  11. +        Membri non pubblici        
  12.         ConnectionString    "{Microsoft Access Driver (*.mdb)};DSN=.\\DataBase\\BRP.mdb;Uid=Admin;Pwd=;"    string
  13.  
Mar 23 '10 #7

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

Similar topics

7
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...
4
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...
2
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. ...
3
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...
1
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. ...
3
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...
4
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...
2
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...
1
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...
3
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 =...
1
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...
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...
0
isladogs
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...

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.