Manoj Singh
Manoj Singh

Reputation: 7707

Insert only unique value in table through vb.net application

I am using vb.net code and sql server 2005.

I have textbox in my application which is linked to a sql column. Now I want to check that it should not insert any duplicate record in table. I mean everytime it insert in table the value should be unique. data is inserted in table on button click.

I am using objectdatasource for sql connectivity and FormView for dataentry.

Please suggest! with some example code!

Thanks.

Best Regards, MS

Upvotes: 0

Views: 1324

Answers (2)

Asim Sajjad
Asim Sajjad

Reputation: 2534

If the column you are talking about is of type Integer then you can set it to primary key and then use the property of that column , set the identity to true and set identity seed and Identity increment to 1 1 , you don't need to insert that value hope this will help.

Upvotes: 0

Daniel Brückner
Daniel Brückner

Reputation: 59675

The simplest solution is probably to add a unique constraint to your table. If you then try to insert a duplicate item, you will get an exception that you can catch and handle.

The cleaner solution is to add the unique constraint and query the database for the item you want to insert. If it is already there, do nothing, else perform the insert.

Upvotes: 1

Related Questions