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

半腔热情 提交于 2019-12-25 02:56:01

问题


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.


回答1:


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;
}



回答2:


Try This

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



来源:https://stackoverflow.com/questions/24337996/oledb-exception-in-c-sharp-no-value-given-for-one-or-more-required-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!