Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to make the system check that the number in the label, correlates with one in the database ( similar concept to a login page) However the user shouldnt need to enter any information but I keep on getting errors...

OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] WHERE [Order ID] = @[Order ID]", MAcon);
da.SelectCommand.Parameters.AddWithValue("@[Order ID]", PrdtID.Text);
DataTable dtbl = new DataTable();
da.Fill(dtbl);


What I have tried:

OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] WHERE [Order ID] = @[Order ID]", MAcon);
da.SelectCommand.Parameters.AddWithValue("@[Order ID]", PrdtID.Text);
DataTable dtbl = new DataTable();
da.Fill(dtbl);
Posted
Updated 11-Apr-18 17:54pm
Comments
[no name] 11-Apr-18 19:44pm    
And what is your question?
Wendelius 11-Apr-18 23:32pm    
What is the error you get?

1 solution

Use your debugger - check the statement, does it make sense, can you execute it via another query tool against the DB?
Likely issues are;
First issue - why are you naming your parameter "@[Order ID]"?
Parameter names should not contain spaces, nor should they use [] brackets. the [] brackets are used when querying columns that contain spaces or reserved words.
Change your parameter name to; @OrderId
Secondly, you may find that AddWithValue is interpreting your value as an incorrect data type, hence your statement ends up as;
SQL
SELECT * FROM [Customer Orders] WHERE [Order ID] = '2'

when you actually want
SQL
SELECT * FROM [Customer Orders] WHERE [Order ID] = 2

Depending on the database engine you are using this may or may not be automatically converted - MS SQL will normally return correctly but MS Access does not.
Either convert the string value to the correct data type - most likely an integer, or use;
C#
da.SelectCommand.Add("@OrderId", OleDbType.Integer).Value = 2

This will ensure your statement gets correctly created.

Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900