We help IT Professionals succeed at work.

Odbc connection, parameter error

osiris247 asked
Last Modified: 2008-01-09
Hi All

I am trying to query an access database and am getting problems before i really get going with this project!
Normally i am connecting to SQL so odbc is new ground to me, however i thought that handling connections was the same in .net.

I am doing the following...
logonCmd.CommandText = "SELECT intSystemAccess FROM SystemUsers WHERE chrUserName = @uName AND chrPassword = @pWord1";
            logonCmd.Parameters.Add("@uName", OdbcType.VarChar, 50).Value = txtUname.Text;
            logonCmd.Parameters.Add("@pWord", OdbcType.VarChar, 50).Value = txtPword.Text;
//normally i user logonCmd.Paramter.AddWithValue("@uName", txtUname.Text - but thought i would try the above after it //addWithValue failed.

I get Error 07002 Microsoft ODBC Access Driver - Too few parameters.  Expected 2

When i do this...
logonCmd.CommandText = "SELECT intSystemAccess FROM SystemUsers WHERE chrUserName = '" + txtUname.Text + "' AND chrPassword = '" + txtPword.Text + "'";

It works fine??!! Can anyone shed any light on where i am going wrong?

Many Thanks
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
Hi osiris247,

Out of curiousity, does it work if you change to the following longer-winded approach?

    OdbcParameter uNameParameter = new OdbcParameter("@uName", OdbcType.VarChar, 50);
    OdbcParameter pWordParameter = new OdbcParameter("@pWord", OdbcType.VarChar, 50);  
    uNameParameter.Value = txtUname.Text;
    uNameParameter.Value = txtPword.Text;




You were right all i needed to so was change the @uName in the sql string to ? and it worked.

logonCmd.CommandText = "SELECT intSystemAccess FROM SystemUsers WHERE chrUserName = ? AND chrPassword = ?";
logonCmd.Parameters.AddWithValue("?", txtUname.Text);
logonCmd.Parameters.AddWithValue("?", txtPword.Text);

Thanks senior for your input as well.


Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.