简体   繁体   中英

OleDb Exception in c# “No value given for one or more required parameters.” while trying to Select Data from Access database

I have the following code:

 public DataTable opencon(PAL.property objpal)
    {
        string query = "Select UserId,Firstname,UserType from TBL_USER_LOGIN where Username=@username and Password=@password and Status=1";
        OleDbCommand objcmd = new OleDbCommand();
        objcmd.CommandText = query;
        objcmd.Connection = oldbcon;
        oldbcon.Open();
        objcmd.Parameters.Add("@username", OleDbType.VarChar).Value = objpal.username;
        objcmd.Parameters.Add("@password", OleDbType.VarChar).Value = objpal.Password;
        DataTable dt = new DataTable();
        OleDbDataAdapter adp = new OleDbDataAdapter(objcmd);
        adp.Fill(dt);
        return dt;
    }

Here I want to fetch some values from the table according to a condition, but when I run this code it shows the following error: 在此处输入图片说明

Even though I passed the correct parameters value in @username and @password. How can I resolve this error? Please help.

Try This:

public DataTable opencon(PAL.property objpal)
{
    string query = "Select UserId,Firstname,UserType from TBL_USER_LOGIN where Username=? and Password=? and Status=1";
    OleDbCommand objcmd = new OleDbCommand();
    objcmd.CommandText = query;
    objcmd.Connection = oldbcon;
    oldbcon.Open();
    objcmd.Parameters.Add("@username", OleDbType.VarChar).Value = objpal.username;
    objcmd.Parameters.Add("@password", OleDbType.VarChar).Value = objpal.Password;
    DataTable dt = new DataTable();
    OleDbDataAdapter adp = new OleDbDataAdapter(objcmd);
    adp.Fill(dt);
    return dt;
}

Try This

string query = "Select [UserId],[Firstname],[UserType] from [TBL_USER_LOGIN] where [Username]=? and [Password]=? and [Status]=1";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM