none
ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. RRS feed

  • Question

  • Hi all,

    I had created a simple log in screen with Employee ID and Password.

    But once I execute the code, it will give me an error as shown above at the ExecuteReader section.

    My code is as follow . Can anyone please help?

     

    private void button1_Click(object sender, EventArgs e)

    {

    string test = "";

    bool boolReturnValue = false;

    string UserName=txtEmpID.Text;

    string Password=mtxtPwd.Text;

    string strConnection = "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\test\\Login.mdb;Exclusive=1;Uid=;Pwd=;";

    //SqlConnection Connection = new SqlConnection(strConnection);

    String strSQL = "Select count(*) From Users where UserName ='"+UserName+"' and Password = '"+Password+"'";

    MessageBox.Show(strSQL);

    OdbcConnection conn = new OdbcConnection(strConnection);

    // Create command object

    OdbcCommand cmd = new OdbcCommand(strSQL);

    cmd.Connection = conn;

    // Open connection

    conn.Open();

    OdbcDataReader reader = cmd.ExecuteReader();

    while (reader.Read())

    {

    test = reader.GetValue(0).ToString();

    }

    }

     

    Monday, February 6, 2006 6:04 AM

Answers

  • Odds are that either UserName or Password is not a column in the Users table. When a SQL statement run against Access has a badly named column, Access thinks you've defined a named parameter and is looking for something in the Parameters collection.

    If that is not the case, then try putting square brackets around 'Users' in the statement. Something in my past niggles that Users might be a key word.

    Hope that helps.

    Monday, February 6, 2006 6:33 AM
  • Thanks for your reply. I got it. It is due to wrong column name.
    Monday, February 6, 2006 7:17 AM

All replies

  • Odds are that either UserName or Password is not a column in the Users table. When a SQL statement run against Access has a badly named column, Access thinks you've defined a named parameter and is looking for something in the Parameters collection.

    If that is not the case, then try putting square brackets around 'Users' in the statement. Something in my past niggles that Users might be a key word.

    Hope that helps.

    Monday, February 6, 2006 6:33 AM
  • Thanks for your reply. I got it. It is due to wrong column name.
    Monday, February 6, 2006 7:17 AM
  • Hi!
    I use this method to access to txt files. And it get me this error. Can you help me?
    Monday, April 23, 2007 6:20 AM
  • I am getting this same error message. I have checked my code and the column names are correct. I even copied and pasted the sql into the query tool and, after replacing the two variables with the same data the application is trying to use, it returns the correct row with no error. I think my problem may be with adding parameters but I don't know what I am doing wrong (forgive me, I am a reforming java developer).  Here is the relevant code:

     

    Code Snippet

    OdbcCommand cmd = new OdbcCommand("select * from Users where user_id=@UserID and passwd=@Password", conn);

    cmd.Parameters.Add(new OdbcParameter("@UserID", userid));

    cmd.Parameters.Add(new OdbcParameter("@Password", password));

    OdbcDataReader reader = cmd.ExecuteReader();

     

     

    My exact error message from the debugger is: +  [System.Data.Odbc.OdbcException] {"ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2."} System.Data.Odbc.OdbcException
    Tuesday, April 1, 2008 11:00 PM
  • If memory serves, when going against an Access database, you can't use named parameters. Instead use @1 and @2 as the parameter names.

     

    Hope that helps.

     

     

    Wednesday, April 2, 2008 12:06 AM
  • Odds are that either UserName or Password is not a column in the Users table. When a SQL statement run against Access has a badly named column, Access thinks you've defined a named parameter and is looking for something in the Parameters collection.

    If that is not the case, then try putting square brackets around 'Users' in the statement. Something in my past niggles that Users might be a key word.

    Hope that helps.


    Jip this sorted my problem too... Thanks.
    Just shows you old forums are not always OLD...
    Coffee is what makes civilised life Possible
    • Proposed as answer by Justus Burger Sunday, January 24, 2010 7:47 PM
    Tuesday, December 22, 2009 7:24 AM
  • Damn str8...And when you've been coding for 20 hours straight, all your incorrect queries start to look flawless...but btw it solved my proplem aswell. Happy coding my fellow geeks...

    Sunday, January 24, 2010 7:49 PM
  • Hey, I have a similair issue - trying to read in data stored in an excel spreadsheet and store it in a backend SQL Server database; problem is that the two Phone columns (User Phone, Contact Phone) both return NULL when the data is a space-delimited number format (ie: 11 123 4567).


    Running this query gives me the "Too few parameters" error (oh how very specific, microsoft)

    SELECT Location, Department, [Site Contact], {fn LCASE([Contact Phone])}, [Contact Email], User, {fn LCASE([User Phone])}, [User Email], Type, Manufacturer, Model, [Serial Number], [Asset Number], [Purchase Date], Condition, Surplus FROM [Assets$]

    Any ideas would be fantastic - I get the same error when trying to use CONVERT in place of LCASE, and trying SELECT CSTR([User Phone]) returns "<cannot read data>"

    Thursday, July 5, 2012 2:33 AM
  • hello to all,

    just as help. I had the same issue, So I changed the parameters name to question Mark

    it works fine; for example:

    OdbcCommand cmd = new OdbcCommand("select * from Users where user_id=? and passwd=?", conn);

    cmd.Parameters.Add(new OdbcParameter("@UserID", userid));

    cmd.Parameters.Add(new OdbcParameter("@Password", password));

    OdbcDataReader reader = cmd.ExecuteReader();

    Good luck

    Thursday, September 13, 2012 4:15 AM