简体   繁体   中英

How to get Max id From Database using c#?

cmd = new OleDbCommand("SELECT max(substr(tr_refno,9,6))as REFID from ECHALLAN WHERE DEPT='" +tmpDept.ToString() + "' and substr(tr_refno,5,2) ='" + Tmpmonth + "'", con);                         
maxid = Convert.ToInt16( cmd.ExecuteScalar());

Error= InvalidCastException was unhandled by user code

Before Converting it you could check the nullity:

var value = cmd.ExecuteScalar();
int maxI;
If(value !=null)
   maxId = Convert.ToInt32(value);
else 
//......................

试试这个命令:

cmd = new OleDbCommand("SELECT Max(COALESCE(substr(tr_refno,9,6),0)) as REFID from ECHALLAN WHERE DEPT='" +tmpDept.ToString() + "' and substr(tr_refno,5,2) ='" + Tmpmonth + "'", con);      

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