check if the primary key already exists?

LewisY95

New member
Joined
Sep 10, 2013
Messages
2
Programming Experience
Beginner
fixed - created new method and called
 
Last edited:
Is there a specific reason that you're generating the PK yourself? Does that data have some specific meaning or is it just an identifier? If it's the latter then you should just let the database create it for you and not care what it is.

If you really do need to create it yourself then you would query the database and use a WHERE clause to filter out all non-matching records but, instead of getting the data itself, you get a count of the data. You can then create a command and call ExecuteScalar, which is specifically for retrieving a single value. That value will be zero if there are no matches and non-zero otherwise.
 
Is there a specific reason that you're generating the PK yourself? Does that data have some specific meaning or is it just an identifier? If it's the latter then you should just let the database create it for you and not care what it is.

If you really do need to create it yourself then you would query the database and use a WHERE clause to filter out all non-matching records but, instead of getting the data itself, you get a count of the data. You can then create a command and call ExecuteScalar, which is specifically for retrieving a single value. That value will be zero if there are no matches and non-zero otherwise.


All the tables are in the Database, it's just a simple form, that displays data from the database in a format of a Datagrid. Whenever I click a cell it'll put the data in the correct column/row into the text boxes so you can update, or create a new and throw it back into the database.

However, if you wanted to create a new one called... "TEST" and TEST already existed in the database, it'll tell you that there is already an existing item in the Database because a PK is unique and will not allow the same values repeated.

So, what I want to do rather than crashing the program, I want to perform a method saying that TEST already exists in the database, please choose a different name. And I also want to be able to use this so that in a different table from the database will check if TEST exists in one table before it can perform its method.

So like a relationship, in order for more information to be created it must check if 'TEST' exists in the database.. If not. then create it.
 
I asked you questions that you haven't answered and I told you what to do if you must provide the PK yourself. You quoted my post but it doesn't seem as though you read it.
 
Back
Top