Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dim ds AS String = ""

ds = sqlFunction.ExecuteSQL("INSERT INTO employee.empdata (empid,fname,lname,cnumber) VALUES ('" & empid.text & "','" & fname.text & "', '" & name.text & "','" & cnumber.text & "') ",sqlFunction.connstring)


What I have tried:

What i want is to ensure that i don't insert the same empid in my database
Posted
Updated 13-Dec-18 19:13pm
v2
Comments
ChauhanAjay 13-Dec-18 23:12pm    
Please check if the empid exists in your table or not. If it does not exists then insert the data.
Member 14076249 13-Dec-18 23:59pm    
what i want is to set a query that checks or display if the data that you have entered is already in the database
Bryian Tan 13-Dec-18 23:29pm    
empid is primary key?
Member 14076249 14-Dec-18 0:02am    
Bryian, yes empid is primary
Bryian Tan 14-Dec-18 0:25am    
if yes, then how the table end up with duplicates?

1. If EmpID is the Primary Key, you will not be allowed to insert a duplicate. This should throw an error reflecting the constraint violation.

2. Insert, Update, and Delete statements will only return an integer reflecting the amount of rows affected. If the value is 0, the row was not inserted.

3. NEVER EVER concatenate a string together to make your command. You should be utilizing a parameterized statement to avoid SQL Injection, which was identified 20 years ago and is still in the top 5 website vulnerabilities. Even a non-malicious entry will break your command; try using a last name of O'Rourke and see what happens

Now for some simple answers to your question
A. Try running a SELECT command with a WHERE clause for the EmpID you are checking
B. Use a Stored Procedure, which can contain the SELECT statement from (A) and only insert if it does not exist. A stored procedure can also contain OUTPUT parameters which you can pass more information back to the program
 
Share this answer
 
Not clear how your application being structure, but there are several way to do it,
1. Try, catch statement, assuming the insert query will complaint if inserting duplicate key: example Using Try... Catch..., Finally![^]
2. Write another select statement to check if the empid exists before inserting

Also the code might vulnerable to SQL injection/ cross site scripting attack. Here is an example how the mentioned vulnerability works.
SQL Injection and Cross-Site Scripting[^]

Try use Parameterized Queries if possible
Using Parameterized Queries and Reports in VB.NET Database Applications[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900