I have a database which stores video game details.The table name is games & its primary key is ‘gameNo’, which is alphanumeric(ex. G1,G2,G3…. etc.). I want to retrieve the last entered game’s number in order to generate the game number for the next game, when it is entered. When I use,
this.cmd = new OleDbCommand("SELECT MAX(gameNo) FROM games",this.con.conObj());
i get incorrect results. For example if there are gameNo values like G2 & G190, I want to retrieve G190, instead I get G2. Please any help would be greatly appreciated. I’m a student & if possible a good explanation would be a huge help too. Thanks in advance. By the way i’m using Visual C#.
This is a consequence of storing the key as a varchar column.
I would separate out the ‘G’ from the Number and have two columns. If it is always ‘G’ then you dont even need the two columns.
Also you have another problem which is caused by using the
SELECT MAX(gameNo) FROM gamesto get the last game number. If this system is a multi user system then two users could get the same number. You didn’t mention which database you where using but all databases will let you safely get a unique id for a game.I would have a table something like (This is for SQL Server but you will get the idea.
Then when you create the new row for the game the database will handle giving you a new GameID.