473,405 Members | 2,171 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,405 software developers and data experts.

OdbcDataReader.Read() returns false

I'm attempting to execute a SQL SELECT query but it doesn't seem to be returning any records. I've confirmed that the connection is actually opening and when I tried to execute the same query on mysqL server it returns results. but when I execute the query in my project without using OdbcParameter, it returns the result as expected.
Here is my code...

Expand|Select|Wrap|Line Numbers
  1.             // connection string declaration
  2.             string connstr = @"Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=logsdb; User=root;Password=12345;Option=3;";
  3.             // instantiate the connection
  4.             OdbcConnection odbc_conn = new OdbcConnection(connstr);
  5.             OdbcDataReader odbc_reader=null;
  6.  
  7.                 // open the connection
  8.                 odbc_conn.Open();
  9.                 string query_stmt = "SELECT * FROM logsdb.userslog  WHERE (uname=@user & pw=@password )";
  10.  
  11.                 // Declare command object with paramater
  12.                 OdbcCommand odbc_command = new OdbcCommand(query_stmt, odbc_conn);
  13.                 // define the paramaters used in command object
  14.                 OdbcParameter user_param = new OdbcParameter("@user", TextBoxusr.Text);
  15.                 OdbcParameter pw_param = new OdbcParameter("@password", TextBoxpw.Text);
  16.                 // add paramater to command object
  17.                 odbc_command.Parameters.Add(user_param);
  18.                 odbc_command.Parameters.Add(pw_param);
  19.                 // get the query result
  20.                 odbc_reader = odbc_command.ExecuteReader();
  21.                 // check the query result
  22.                 if (odbc_reader.Read() )
  23.                 {
  24.                     string user=(string)odbc_reader["uname"];
  25.                     string pw= (string)odbc_reader["pw"];
  26.                 }
  27.              finally
  28.              {
  29.                 if (odbc_reader != null)
  30.                     odbc_reader.Close();
  31.                 if (odbc_conn != null)
  32.                     odbc_conn.Close();
  33.              }
So why odbc_reader.Read() returns false?
Jul 9 '08 #1
5 2150
kenobewan
4,871 Expert 4TB
Are uname=@user & pw=@password correct? Are your field names really uname & pw? Have you used validation?
Jul 9 '08 #2
Are uname=@user & pw=@password correct? Are your field names really uname & pw? Have you used validation?
yes, fields names are uname and pw,I tried to execute the same query ( without using parameters ) and it returns results.
like this:
string query_stmt = @"SELECT * FROM logsdb.userslog WHERE uname= '" + TextBoxusr.Text + "'"+ "& pw='"+TextBoxpw.Text+"'";
OdbcCommand odbc_command = new OdbcCommand(query_stmt, odbc_conn);

I know that this is a bad query cuz it invites hackers to replace that textBox string value with something malicious.

so, why when I use OdbcParameter with OdbcDataReader returns false?????

pls can anyone help me.......
Jul 9 '08 #3
kenobewan
4,871 Expert 4TB
ODBC parameters are not named, they are positional and question marks are used as placeholders. The parameters must be added in the order they appear in your quert.

Here is an example:
Expand|Select|Wrap|Line Numbers
  1. myCommand.CommandText="SELECT * FROM Table WHERE Colomn = ?" 
  2.  
  3. int value = 4; 
  4.  
  5. OdbcParameter myParameter = new OdbcParameter(); 
  6.  
  7. myParameter.DbType = DbType.Int32; 
  8.  
  9. myParameter.OdbcType = OdbcType.Int; 
  10.  
  11. myParameter.Size = 4; 
  12.  
  13. myParameter.Value=value;
  14.  
  15. myCommand.Parameters.Add(myParameter);
  16.  
Jul 10 '08 #4
ODBC parameters are not named, they are positional and question marks are used as placeholders. The parameters must be added in the order they appear in your quert.

Here is an example:
Expand|Select|Wrap|Line Numbers
  1. myCommand.CommandText="SELECT * FROM Table WHERE Colomn = ?" 
  2.  
  3. int value = 4; 
  4.  
  5. OdbcParameter myParameter = new OdbcParameter(); 
  6.  
  7. myParameter.DbType = DbType.Int32; 
  8.  
  9. myParameter.OdbcType = OdbcType.Int; 
  10.  
  11. myParameter.Size = 4; 
  12.  
  13. myParameter.Value=value;
  14.  
  15. myCommand.Parameters.Add(myParameter);
  16.  
kenobewan
WOW!!! You are wonderful. Thanks a million
Jul 12 '08 #5
kenobewan
4,871 Expert 4TB
kenobewan
WOW!!! You are wonderful. Thanks a million
No problem, 999,999 thanks would have been sufficient ;).
Jul 12 '08 #6

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

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
75
by: Greg McIntyre | last post by:
I have a Python snippet: f = open("blah.txt", "r") while True: c = f.read(1) if c == '': break # EOF # ... work on c Is some way to make this code more compact and simple? It's a bit...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
1
by: cyshao | last post by:
OdbcDataReader How to get UInt32 ? My DB item defined as int(10) , a value is 108371984. It's larger then Int32.MaxValue, but OdbcDataReader seems don't provide GetUInt32(); I tried to use...
6
by: Steven Blair | last post by:
Hi, I am writing an application using a 3 tier model (Client, Business Layer and DB) The DB layer creates a OdbcDataReader object back up to the Client where the data is read and displayed on...
1
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
2
by: pagates | last post by:
Hi All, Just looking for a confirmation on something (in VS 2003). Can an OdbcDataReader be populated only by a ExecuteReader on an OdbcCommand? Thanks, pagates
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
0
by: vishnu | last post by:
Hi, Am trying to post the data over https and am getting error in httpwebresponse.getResponseStream.Please help me to get rid of this issue. Here is the message from immediate window ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.