mysql - "The given key was not present in the dictionary" when Execute the mysqldatareader in C# -
i have run command without error in mysql. other command run fine code not work. know happen code.
private static user getuser(mysqlcommand cmd) { user usr = new user(); mysqldatareader rdr = cmd.executereader(); if (rdr.hasrows) { while (rdr.read()) { usr.id = convert.toint32(rdr["id"]); usr.level = (level)enum.parse(typeof(level), rdr["level"].tostring()); usr.email = rdr["email"].tostring(); usr.createddate = convert.todatetime(rdr["createddate"].tostring()); usr.lastlogindate = convert.todatetime(rdr["lastlogindate"].tostring()); } } return usr; } public static user getuserfromid(int userid) { string qry = "select * user id = ?userid"; user user = new user(); mysqlconnection cnn = new mysqlconnection(globals.connstring); cnn.open(); using (cnn) { mysqlcommand cmd = new mysqlcommand(qry, cnn); cmd.parameters.addwithvalue("userid", userid); user = getuser(cmd); } cnn.close(); return user; } the code paste here gave me error
"the given key not present in dictionary."
on line of line 158:
mysqldatareader rdr = cmd.executereader(); do know wrong happen code? have added charset=utf8; in connectionstring people suggest in so.
the database use mariadb , connector mysql latest connector. know if have trouble.
i have no problem while run other function. problem happen in single function use mysqldatareader execution.
you use @ instead of ? calling parameter sql should called in sql as: @userid (whatever type is, varchar, int etc)
so sql proc should this
select * (tablename) userid = @userid so in form call @userid parameter passed in sql.
Comments
Post a Comment