In a web project, I'm trying to execute the following query:
SELECT ItemName as Name,
ItemPicture as Picture,
ItemHeroModif as Assistance,
ItemTroopModif as Charisma,
HerbCost as Herbs,
GemCost as Gems
FROM Item WHERE ItemId = @value0
With breakpoints, I can see I attached to @value0 the value, 2.
Despite this, I get the following error:
No value given for one or more required parameters.
I understood this error is usually generated due to bad SQL syntax. Is there anything wrong with what I did?
EDIT:
Attachment code:
var madeForCommand = "SELECT ItemName as Name,ItemPicture as [Picture],ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems FROM Item WHERE ";
OleDbCommand command = new OleDbCommand();
for (int ii = 0; ii < items.Count; ii++)// items is a list of items with IDs I want to get from the query.
{
madeForCommand += "ItemId =@value"+ii+" OR ";
}
madeForCommand = madeForCommand.Substring(0, madeForCommand.Length - 4); // making sure I trim the final or; In the case I shown, it's just one item, so there are none at all.
And later on:
OleDbCommand forOperations = new OleDbCommand(madeForCommand, _dbConnection); //_dbConnection is the connection to the database, it seems to work pretty well.
for (int ii = 0; ii < items.Count; ii++)
{
string attach = "@value" + ii;
command.Parameters.AddWithValue(attach, items[ii].ID);
}
I'm pretty sure items[ii].ID is fine, breakpoints show that it equals 2 and the attachment goes well.
EDIT 2: I've editted the code as Krish and Hans advised me, and I get the following query without any attachments:
SELECT ItemName as [Name],ItemPicture as Picture,ItemHeroModif as Assistance,ItemTroopModif as Charisma, HerbCost as Herbs, GemCost as Gems FROM [Item] WHERE (ItemID in (2));
I still get the same error, if it changes anything.
EDIT 3: Executing the query in Access asks me to give a value to the parameter "ItemPicture"... Odd; ItemPicture is a column, isn't it?
@value0to2? That is probably the problem. Also, sometimes you set a parameter tonulland get the same error.