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
cmd.ExecuteScalar()looks like when you debug it? And please use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.IDataParameter: msdn.microsoft.com/en-us/library/…