473,625 Members | 3,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parameters in SQL Statement not working

I'm writing a database client program in C#, and it accesses a MS SQL V7
database. In one part of my program I am using a SqlCommand class to run a
SQL Statement, the command text uses parameters, but these do not seem to be
functioning (as it does not find the record), it's probably some really
stupid error I've made, but I've spend several hours trying to find it and
its now driving me crazy. Also something very similar else where in the
program works fine.

The table I'm using is called Suppliers and it has a field called
AccountCode and on of the records has the data value of Rice in the
AccountCode field.

If I write the statement so that it is as follows, the code finds the
record:
sqlCommand.Comm andText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE AccountCode = 'Rice';";

But when using parameters it doesn't work, here is the code with parameters:

SqlCommand sqlCommand = new SqlCommand("", dbConnection);
SqlDataReader dReader;
sqlCommand.Comm andType = CommandType.Tex t;

SqlParameter param1 = new SqlParameter("@ searchField", "AccountCod e");
sqlCommand.Para meters.Add(para m1);
SqlParameter param2 = new SqlParameter("@ searchString", "Rice");
sqlCommand.Para meters.Add(para m2);

sqlCommand.Comm andText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE @searchField = '@searchString' ;";
// Also tried the @searchString with out the ' ' each side of it
//" WHERE AccountCode = 'Rice' ; "; // It works if modifed to
this
dReader = sqlCommand.Exec uteReader();
if(dReader.HasR ows == true)
{
.....
}

Anyone have any ideas why the parameter version doesn't work?
No exceptions are thrown, the data reader just doesn't return any data.

Thank You for any help,
Grant
Nov 15 '05 #1
2 10860
Parameters are used with stored procedures.
I do not understand why are you trying to use paramaters with
CommandType.Tex t.

"Grant Stanley" <ag**@kent.ac.u k.delete> wrote in message
news:rQ******** ************@br ightview.com...
I'm writing a database client program in C#, and it accesses a MS SQL V7
database. In one part of my program I am using a SqlCommand class to run a
SQL Statement, the command text uses parameters, but these do not seem to be functioning (as it does not find the record), it's probably some really
stupid error I've made, but I've spend several hours trying to find it and its now driving me crazy. Also something very similar else where in the
program works fine.

The table I'm using is called Suppliers and it has a field called
AccountCode and on of the records has the data value of Rice in the
AccountCode field.

If I write the statement so that it is as follows, the code finds the
record:
sqlCommand.Comm andText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE AccountCode = 'Rice';";

But when using parameters it doesn't work, here is the code with parameters:
SqlCommand sqlCommand = new SqlCommand("", dbConnection);
SqlDataReader dReader;
sqlCommand.Comm andType = CommandType.Tex t;

SqlParameter param1 = new SqlParameter("@ searchField", "AccountCod e");
sqlCommand.Para meters.Add(para m1);
SqlParameter param2 = new SqlParameter("@ searchString", "Rice");
sqlCommand.Para meters.Add(para m2);

sqlCommand.Comm andText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE @searchField = '@searchString' ;";
// Also tried the @searchString with out the ' ' each side of it
//" WHERE AccountCode = 'Rice' ; "; // It works if modifed to
this
dReader = sqlCommand.Exec uteReader();
if(dReader.HasR ows == true)
{
....
}

Anyone have any ideas why the parameter version doesn't work?
No exceptions are thrown, the data reader just doesn't return any data.

Thank You for any help,
Grant

Nov 15 '05 #2
My guess is that you can use parameters only for literal values and not for
field names. You can't treat your query as a sort of macro expression where
parameter values substitute their placeholders no matter what they mean.
Rather parameters are used in run-time to pass values to the Select
statements or stored procedures.

HTH,

Eliyahu

"Grant Stanley" <ag**@kent.ac.u k.delete> wrote in message
news:rQ******** ************@br ightview.com...
I'm writing a database client program in C#, and it accesses a MS SQL V7
database. In one part of my program I am using a SqlCommand class to run a
SQL Statement, the command text uses parameters, but these do not seem to be functioning (as it does not find the record), it's probably some really
stupid error I've made, but I've spend several hours trying to find it and its now driving me crazy. Also something very similar else where in the
program works fine.

The table I'm using is called Suppliers and it has a field called
AccountCode and on of the records has the data value of Rice in the
AccountCode field.

If I write the statement so that it is as follows, the code finds the
record:
sqlCommand.Comm andText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE AccountCode = 'Rice';";

But when using parameters it doesn't work, here is the code with parameters:
SqlCommand sqlCommand = new SqlCommand("", dbConnection);
SqlDataReader dReader;
sqlCommand.Comm andType = CommandType.Tex t;

SqlParameter param1 = new SqlParameter("@ searchField", "AccountCod e");
sqlCommand.Para meters.Add(para m1);
SqlParameter param2 = new SqlParameter("@ searchString", "Rice");
sqlCommand.Para meters.Add(para m2);

sqlCommand.Comm andText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE @searchField = '@searchString' ;";
// Also tried the @searchString with out the ' ' each side of it
//" WHERE AccountCode = 'Rice' ; "; // It works if modifed to
this
dReader = sqlCommand.Exec uteReader();
if(dReader.HasR ows == true)
{
....
}

Anyone have any ideas why the parameter version doesn't work?
No exceptions are thrown, the data reader just doesn't return any data.

Thank You for any help,
Grant

Nov 15 '05 #3

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

Similar topics

2
1917
by: Joe Lui | last post by:
Hello everyone, I am new to PHP and web server in general so please bear with me if it's an obvious mistake. My problem is that parameters entered in URL do not update php variables. I am using IIS on winxp pro with the latest php installed. PHP seems working fine, i.e. <?php echo '<p>Hello World</p>'; ?> works as expected.
6
4106
by: Arpan | last post by:
Microsoft advises not to pass parameters to the Command object in the Execute statement. Why? Thanks, Arpan
1
1818
by: Gerry Abbott | last post by:
Hi all I'm converting a db from Acc97 to 2000. Everything works, except assigning of a qdf object containing parameters, to a recordset. The db, recordset, and qrydef objects are all referenced as DAO objects, and the reference to dao 3.6 is present and working. The code breaks at the last statement (below) with an 'invalid arguament' error 3001. Note that the parameters are both dates.
7
21612
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
6
4734
by: Jack | last post by:
I have the following: * An OLEDBCommand with command text "SELECT CAMPAIGN, DAY_OUT WHERE (CAMPAIGN LIKE '@campaign')" * A DataAdapter that point the select to the above command * A data grid that I use to display the data * form load code to populate the controls: DsCampaign1 = New DataSet cmdProActiTmp_Sel.Parameters.Item("@Campaign").Value = _ Trim(txtcampaign.Text())
3
2009
by: pjcraig | last post by:
This is driving me crazy! I have a form that a user will access from another form by selecting the item that they wish to view. When they open the new form, I pass through the id of the item they selected so that only the details for the given item are displayed. In order for this to work, I using the following code.... This is working no problems at all: SqlSelectCommand1.Parameters("@blast_no").Value = blastNo So this displays the...
0
1221
by: nigeljordan | last post by:
Hi I’m having problems with the following method… It works for the ‘@user’ bit (i.e. it takes in the user variable and works within the sql command). But the @column variable still is not working somehow. It’s probably something to do with the way I’ve phrased the actual sql statement.
1
2742
by: nigeljordan | last post by:
Hi I’m having problems with the following method… It works for the ‘@user’ bit (i.e. it takes in the user variable and works within the sql command). But the @column variable still is not working somehow. It’s probably something to do with the way I’ve phrased the actual sql statement.
2
2625
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying to update a record, I get a "No value given for one or more parameters." error message. I use a Select with parameters and an Update with parameters. The select works fine. I thought I've tried everything (evidently not) to get this working. ...
0
8256
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.