473,465 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL WHERE Command Help [C# Win]

Hi everyone,

I'm having problems with my WHERE Clause syntax with in my SQL CommandText.
The error that it is display is "You Have No Data". My problem lies with in
the WHERE clause not finding up my passed variable from my get/set function.
I have done watch's on the variable and the data name I'm looking for is
present.

I thought my syntax was correct, but maybe it's not. Can someone look it
over and see if there is anything wrong with it and give me a correct
solution if there is something wrong. Thank you all in advance.

public string Extract_Parameter

{

get

{

return extract_parameter;

}

set

{

extract_parameter = value;

}

}

public void Load_Lunch_Menu_Extract()

{

try

{

//Connetion to DataBase

OleDbConnection myConnection = new OleDbConnection(@"Provider =
Microsoft.Jet.OLEDB.4.0;Data Source = C:\Host 017.mdb");

//Open Connection to DataBase

myConnection.Open();

//SQL Command for myConnection

OleDbCommand myCommand = myConnection.CreateCommand();

//Initialize SQL SELECT command to retrieve data

myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = 'Extract_Parameter'";

//Create DataReader

OleDbDataReader myDataReader = myCommand.ExecuteReader();

//Cycle through data and display

test = new ArrayList();

while(myDataReader.Read())

{

}

//Close Connection to DataBase

myDataReader.Close();

myConnection.Close();

test.Sort(0, test.Count, new ArrayList_Sort());

}

catch(Exception myException)

{

MessageBox.Show(myException.Message);

}

}

}

Thanks,

MikeY
Sep 19 '06 #1
3 2728
Mike,

When you want to parameterize a query, you have to manually add the
parameter to the command, as well as set the value. The command does not
reflect on anything to get the value you are expecting.

What you need to do is this:

OleDbCommand myCommand = myConnection.CreateCommand();

//Initialize SQL SELECT command to retrieve data
myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = '@itemCategory'";

// Add the parameter.
myCommand.Parameters.Add(this.Extract_Parameter);

The call to add should extract a parameter with the appropriate value
and then execute properly.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MikeY" <mi*******@yaho.comwrote in message
news:ut**************@TK2MSFTNGP06.phx.gbl...
Hi everyone,

I'm having problems with my WHERE Clause syntax with in my SQL
CommandText. The error that it is display is "You Have No Data". My
problem lies with in the WHERE clause not finding up my passed variable
from my get/set function. I have done watch's on the variable and the data
name I'm looking for is present.

I thought my syntax was correct, but maybe it's not. Can someone look it
over and see if there is anything wrong with it and give me a correct
solution if there is something wrong. Thank you all in advance.

public string Extract_Parameter

{

get

{

return extract_parameter;

}

set

{

extract_parameter = value;

}

}

public void Load_Lunch_Menu_Extract()

{

try

{

//Connetion to DataBase

OleDbConnection myConnection = new OleDbConnection(@"Provider =
Microsoft.Jet.OLEDB.4.0;Data Source = C:\Host 017.mdb");

//Open Connection to DataBase

myConnection.Open();

//SQL Command for myConnection

OleDbCommand myCommand = myConnection.CreateCommand();

//Initialize SQL SELECT command to retrieve data

myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = 'Extract_Parameter'";

//Create DataReader

OleDbDataReader myDataReader = myCommand.ExecuteReader();

//Cycle through data and display

test = new ArrayList();

while(myDataReader.Read())

{

}

//Close Connection to DataBase

myDataReader.Close();

myConnection.Close();

test.Sort(0, test.Count, new ArrayList_Sort());

}

catch(Exception myException)

{

MessageBox.Show(myException.Message);

}

}

}

Thanks,

MikeY


Sep 19 '06 #2
Hi,
myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = 'Extract_Parameter'";
Here is where your problem is, you are saying that {itemcategory] has to be
"Extran_Parameter"

you could change it to:
myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = '" + Extract_Parameter + "'";
I would suggest you to use parameterized queries it will avoid such problems
and avoid the risk of sql injection

This is how it would looks like:
myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = @cat";
myCommand.Parameters.Add( "@cat" SqlDataType.Varchar, 50).Value =
Extract_Parameter ;


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 19 '06 #3
Just typing an update for others that might need this line of code. Hope
this helps others as these two fellows helped me. Txs Again.

//SQL Command for myConnection

OleDbCommand myCommand = myConnection.CreateCommand();

//Initialize SQL SELECT command to retrieve data

myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = @cat";

myCommand.Parameters.Add(new OleDbParameter ("@cat",Extract_Parameter));

//This Syntax works, but opens your program up too Syntax Attacks "sql
injection"

//myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = '" + Extract_Parameter + "'";

//Create DataReader

OleDbDataReader myDataReader = myCommand.ExecuteReader();

MikeY

"MikeY" <mi*******@yaho.comwrote in message
news:ut**************@TK2MSFTNGP06.phx.gbl...
Hi everyone,

I'm having problems with my WHERE Clause syntax with in my SQL
CommandText. The error that it is display is "You Have No Data". My
problem lies with in the WHERE clause not finding up my passed variable
from my get/set function. I have done watch's on the variable and the data
name I'm looking for is present.

I thought my syntax was correct, but maybe it's not. Can someone look it
over and see if there is anything wrong with it and give me a correct
solution if there is something wrong. Thank you all in advance.

public string Extract_Parameter

{

get

{

return extract_parameter;

}

set

{

extract_parameter = value;

}

}

public void Load_Lunch_Menu_Extract()

{

try

{

//Connetion to DataBase

OleDbConnection myConnection = new OleDbConnection(@"Provider =
Microsoft.Jet.OLEDB.4.0;Data Source = C:\Host 017.mdb");

//Open Connection to DataBase

myConnection.Open();

//SQL Command for myConnection

OleDbCommand myCommand = myConnection.CreateCommand();

//Initialize SQL SELECT command to retrieve data

myCommand.CommandText = "SELECT * FROM [Food Lunch Menu] WHERE
[ItemCatagory] = 'Extract_Parameter'";

//Create DataReader

OleDbDataReader myDataReader = myCommand.ExecuteReader();

//Cycle through data and display

test = new ArrayList();

while(myDataReader.Read())

{

}

//Close Connection to DataBase

myDataReader.Close();

myConnection.Close();

test.Sort(0, test.Count, new ArrayList_Sort());

}

catch(Exception myException)

{

MessageBox.Show(myException.Message);

}

}

}

Thanks,

MikeY


Sep 20 '06 #4

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

Similar topics

3
by: Paolo Scolamacchia | last post by:
Hi everybody, this is my environment : Apache is installed on a Linux server (RedHat9A) and I launch the browser on a PC running WindowsXP to execute php scripts on the Linux server. I would run...
5
by: Oli Howson | last post by:
Ok, I'm using php-win, and have the following file, c:\mail.php <? mail('email@myaddress.com','a test','a php-win cli test'); ?> When I go to start|run or cmd and type... php-win -f...
6
by: EAS | last post by:
Is there any way of using Python to open a command prompt?
16
by: Kenneth McDonald | last post by:
For unfortunate reasons, I'm considering switching back to Win XP (from OS X) as my "main" system. Windows has so many annoyances that I can only compare it to driving in the Bay Area at rush hour...
2
by: Vlado | last post by:
I have db2 7.2 on Win 2000, programing MS VB6.0 I update 1 field on 600 records - successfully, but if i update more then 600 records then i have err. mesage SQL0952N Processing was cancelled...
5
by: Jarod | last post by:
Hey I have already written a program that has a user interface, but I would like to add some command line interface too. So if the user run it like: program.exe paramater1 The program do...
1
by: Ennio-Sr | last post by:
Hi all! I'm writing a script that presents the user with a numbered lines menu, each line corresponding to a <case n> which executes a psql command. As the psql-commands are very similar to each...
6
by: Csaba Gabor | last post by:
I would like to use the AT command under Win XP to schedule (CLI) php.exe. I thought it would be as easy as: AT 7:11 "php c:\phpapps\himom.php". However, this gives me an error. My solution is...
0
by: SAM689532 | last post by:
I have DB2 v7.1.2 on IBM Z/OS and DB2 V8.1.0.36 Level ID 01010106 Build Level s021023 with JDK 1.3.1 on my Windows platform. I am trying to load some Arabic character data to our mainframe ...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
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
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
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,...
0
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...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.