I am trying to get the maximum ID from a database table and want to show it on win form load.
I am using the following query to get the maximum ID.
SELECT ISNULL(MAX(ID),0)+1 FROM StockMain WHERE VRDATE = '2013-01-30'
Above should have to return the maximum ID of today., e.g if I this statement is excutes for the first time it will return me the Value ‘1’. After saving first record on ID = ‘1’ it should give me MAX(ID) = ‘2’. But it returns me the value 1.
Any suggestion or solutions????
Wild guess… but what data type is
VRDATE? Does it include a time component, or is it just a date?If it includes a time component indicating when you saved the record, it won’t pass the
VRDATE = '2013-01-30'check, since this defaults to a time of midnight. Since the times aren’t identical, they are not equal.Instead, try:
Next question… have you considered using an
IDENTITYcolumn instead of managing the ID values manually?