none
Error: [ODBC Microsoft Access Drive] Too few parameters RRS feed

  • Question

  • Where is the error? Newbie to C#, occasional coding, not familiar with C# syntax.

    The access file does not have password.

    Thanks.

    private void btnConnect_Click(object sender, EventArgs e)
            {          
                
                GlobalVariables.Username = lblUsername.Text;
                GlobalVariables.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=" + @"My Access File path;";
    
    
                DatabaseCn = new OdbcConnection(GlobalVariables.ConnectionString);
                bool SuccessfulConnection;
    
                SuccessfulConnection = judge(lblUsername.Text);
    
    
    
                if (SuccessfulConnection == false)
                {
                    MessageBox.Show("Connection failed. You are not in User Access table.", "Error Message");
                }
    
                if(SuccessfulConnection==true)
                {
                    DatabaseCn.Close();
                    
                    FormInterface fInterface = new FormInterface();
                    fInterface.Show();
                    this.Close();
                }
                
            }
    
    
    // Check if lblUsername.Text is in the Access table User_access fieldname UserName
    public bool judge(string UserName)
            {
                
                using (OdbcConnection myConn = new OdbcConnection(GlobalVariables.ConnectionString))
                {
    
                    myConn.Open();
                    using (OdbcCommand cmd = new OdbcCommand { Connection = myConn })
                    {
                        cmd.CommandText = "SELECT UserName FROM  User_access WHERE UserName = @GlobalVariables.UserName";
                        cmd.Parameters.AddWithValue("@GlobalVariables.Username", UserName);
                        var reader = cmd.ExecuteScalar();
                        return reader != null;
                    }
                }
            }

    Sunday, November 24, 2019 1:11 AM

Answers

  • Try this name:

        cmd.CommandText = "SELECT UserName FROM  User_access WHERE UserName = @UserName";

       cmd.Parameters.AddWithValue("@Username", UserName);

    or this

       cmd.CommandText = "SELECT UserName FROM  User_access WHERE UserName = ?";

       cmd.Parameters.AddWithValue("@Username", UserName);

    You can find more methods to check the existence of username.


    • Edited by Viorel_MVP Sunday, November 24, 2019 8:03 AM
    • Marked as answer by VA_er Monday, November 25, 2019 4:31 AM
    Sunday, November 24, 2019 8:02 AM