I have created a class that will manage connection and commands, I named it DbConfiguration and it uses a Singleton pattern(using dbc as an object name).
My problem is this SQL syntax throws an error when I try to run ExecuteNonQuery()
Using this connection string:
"Provider=Microsoft.ACE.OLEDB.12.0;dbms.accdb;Persist Security Info=False"
I executed this first:
dbc.SetCommand("INSERT INTO inventory ([CallNumber], [Title], [ImageLocation]) VALUES (@CALLNUMBER, @TITLE, @IMAGELOC);");
dbc.GetCommand().Parameters.Add("@CALLNUMBER", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@TITLE", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@IMAGELOC", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters["@CALLNUMBER"].Value = txtCallNumber.Text;
dbc.GetCommand().Parameters["@TITLE"].Value = txtTitle.Text;
dbc.GetCommand().Parameters["@IMAGELOC"].Value = (!moveto.Equals("db_storage\\") ? moveto : "db_storage\\no_img.png");
and followed by:
dbc.GetCommand().ExecuteNonQuery();
Next is I executed this code:
dbc.SetCommand("INSERT INTO publishing_info ([ID], [Author], [DateTime], [Publisher], [Pages], [ISBN_NO]) " +
"VALUES (" +
"SELECT([ID] FROM inventory " +
"WHERE inventory.CallNumber=@CALLNUMBER AND inventory.Title=@TITLE AND inventory.ImageLocation=@IMAGELOC), " +
"@AUTHOR, @DATETIME, @PUBLISHER, @PAGES, @ISBN);");
and also followed by:
dbc.GetCommand().ExecuteNonQuery();
Here are the parameters that I have used for your reference:
dbc.GetCommand().Parameters.Add("@AUTHOR", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@DATETIME", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@PUBLISHER", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters.Add("@PAGES", System.Data.OleDb.OleDbType.UnsignedInt, 4);
dbc.GetCommand().Parameters.Add("@ISBN", System.Data.OleDb.OleDbType.VarChar, 255);
dbc.GetCommand().Parameters["@AUTHOR"].Value = txtAuthor.Text;
dbc.GetCommand().Parameters["@DATETIME"].Value = txtYear.Text;
dbc.GetCommand().Parameters["@PUBLISHER"].Value = txtPublisher.Text;
dbc.GetCommand().Parameters["@PAGES"].Value = uint.Parse(txtPages.Text);
dbc.GetCommand().Parameters["@ISBN"].Value = txtISBN.Text;
Here is the screenshot of my stack trace(I can’t attach it because I only have 1 rep points)
http://i44.tinypic.com/2w49t84.png
What should I do to make the syntax work?
And also do I need to close the connection first before executing another query?
Update 1
I used DMax(\"[ID]\", \"inventory\") to retrieve the last ID but it returns all the fields of inventory and writes it all on the fields that it can be written to, in my C# code but if I write the query on MS Access 2010 it doesn’t do what it does on a C# code. What seems to be the problem?
Update 2
Problem solved with dbc.GetCommand().Parameters.Clear()
You do not need to close the connection, but you do need to insert a left parenthesis in your sub-select: