473,657 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

select statement with variables

5 New Member
I am trying to populate a datagrid from a select statement that contains variables. I can query the dbf file and get the whole file but when I try a select statement with variables from text box and combo box input the datagrid has the field names at the top but no data here is my code.
Expand|Select|Wrap|Line Numbers
  1. private void button2_Click ( object sender , EventArgs e )
  2.             {
  3.             string field = comboBox1 . Text;
  4.             string query = textBox1 . Text;
  5.             try
  6.                 {
  7.  
  8.  
  9.                 conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + fullPath2 + "';Extended Properties=dBase IV";
  10.                 conn = new OleDbConnection ( conString );
  11.                 command = conn . CreateCommand ( );
  12.  
  13.                 dataGridView1 . DataSource = null;
  14.  
  15.                 // create the DataSet
  16.                 DataSet ds = new DataSet ( );
  17.  
  18.                 // open the connection
  19.                 conn . Open ( );
  20.                 // run the query
  21.                 string commandString = "Select * from ZIP.DBF where '" + field + "'='"+query+"'";
  22.                 command . CommandText = commandString;
  23.                 OleDbDataAdapter adapter = new OleDbDataAdapter (command);
  24.  
  25.  
  26.                 adapter . Fill ( ds );
  27.  
  28.                 // close the connection
  29.                 conn . Close ( );
  30.  
  31.                 // set the grid's data source
  32.                 dataGridView1 . DataSource = ds . Tables [ 0 ];
  33.                 }
  34.             catch ( Exception ex )
  35.                 {
  36.                 MessageBox . Show ( ex . Message );
  37.  
  38.                 }
  39.  
  40.             }
  41.  
THANKS FOR ALL OF YOUR HELP

JOHN LARSON
Nov 7 '07 #1
2 1415
Plater
7,872 Recognized Expert Expert
Try this instead:
Expand|Select|Wrap|Line Numbers
  1. string commandString = "Select * from ZIP.DBF where [" + field + "]='"+query+"'";
  2.  
If the column is of a numerical type, you shouldn't use ' around your value that you're testing for.
Nov 7 '07 #2
balabaster
797 Recognized Expert Contributor
Try this instead:
Expand|Select|Wrap|Line Numbers
  1. string commandString = "Select * from ZIP.DBF where [" + field + "]='"+query+"'";
  2.  
If the column is of a numerical type, you shouldn't use ' around your value that you're testing for.
Er this is a windows forms application, right? If not, you need to be very careful with how you format your queries and should always be using parameters to pass variable data into your query and not appending query strings otherwise you leave yourself susceptible to SQL query injections.
Nov 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
15464
by: Ed Garcia | last post by:
I have a form that sends an ASP page the data to use for this string... using values for "startdate", "enddate" and "lookfor" varibables... rs.Open "SELECT * FROM TABLE WHERE dateadded >= ' & startdate & ' AND dateadded < ' & enddate & ' AND sendemail = ' & lookfor & '" well it works perfectly when I have the actual dates or the actual value of "lookfor" like this example rs.Open "SELECT * FROM TABLE WHERE dateadded >= ('6/15/2003...
0
10187
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the database table, using dynamic sql. Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the error code -2149 is returned That means "Specified partition does not exist". Does anybody know if it is a database problem or a problem of
5
6892
by: Daniel Wetzler | last post by:
Dear MSSQL experts, I use MSSQL 2000 and encountered a strange problem wqhile I tried to use a select into statement . If I perform the command command below I get only one dataset which has the described properties. If I use the same statement in a select into statement (see the second select) I get several datasets with the described properties like I didn't use distinct
1
21666
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan" case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb" End Select
0
1731
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the database table, using dynamic sql. Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the error code -2149 is returned That means "Specified partition does not exist". Does anybody know if it is a database problem or a problem of
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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...
1
8503
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
7327
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
6164
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
4152
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
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1955
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.