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

parameterized queries in C# - Must declare the variable

I am using the following query:
query = "insert into SystemEventsProperties (ID,PName,PValue) values
(@id,@pName,@pValue)";

OdbcParameter param1 = new OdbcParameter(); param1.DbType =
DbType.Int32;

param1.ParameterName = "@id"; param1.Value = maxId;

OdbcParameter param2 = new OdbcParameter(); param2.DbType =
DbType.String;

param2.ParameterName = "@pName"; param2.Value = "abc" ;

OdbcParameter param3 = new OdbcParameter(); param3.DbType =
DbType.String;

param3.ParameterName = "@pValue"; param3.Value = "def";

cmd.Parameters.Add(param1); cmd.Parameters.Add(param2);
cmd.Parameters.Add(param3);

cmd.CommandText = query;

cmd.ExecuteNonQuery(); //AT THIS LINE, IT THROWS AN EXCEPTION THAT SAYS
"ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare
the variable '@id'

What is the problem? Thanx in advance


Nov 16 '05 #1
1 8211
It looks like you're using the CommandType = Text (which is the default)
in which case:

"The ODBC .NET Provider does not support named parameters for passing
parameters to an SQL statement or a stored procedure called by an
OdbcCommand when CommandType is set to Text. In this case, the question
mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?"

Taken from the Visual Studio help... I wish I could somehow send you a
help link.

If you're only writing against SQL Server, you could use the
System.Data.SqlClient namespace instead of the general ODBC one. It has
significantly nicer funtionality.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
8
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf =...
2
by: deko | last post by:
Is it possible to build a parameterized query from another parameterized query? I've tried two variations of this and can't seem to get it to work (using DAO). Any suggestions welcome! I...
1
by: Wajih-ur-Rehman | last post by:
I am using the following query: query = "insert into SystemEventsProperties (ID,PName,PValue) values (@id,@pName,@pValue)"; OdbcParameter param1 = new OdbcParameter(); param1.DbType =...
2
by: Martin | last post by:
Hi, I currently have an application that connects to an MS ACCESS database. This application uses an OLEDB connection string for MS ACCESS. Now, I'd like to upsize the application so I converted...
10
by: ryan.mclean | last post by:
Hi all, I am new to using sql server and parameterized sql. I am hoping to be returned the value of a column that has been inserted. Here is my statement strSqlInsetrtTrack = _ "INSERT INTO...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
2
by: weird0 | last post by:
i write query in this manner by contatenting a string: cmd.CommandText = "SELECT acc_name FROM Account_Information where user_id='" + User_Id + "'"; Is there any other way to do it.... ...
4
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
For MS SQL Server... I am used to declaring local variables in my SQL queries... Declare @MyInt int, @MyChar varchar(33) Parameters were idenfitied with a colon... Where ModDate :MyDate But,...
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: 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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.