?
Solved

Checking database for records before Insertion

Posted on 2005-04-27
7
Medium Priority
?
219 Views
Last Modified: 2012-05-05
Hello All.

 Here is what im trying to do in a nut shell(Using VB.NET):

1. Insert a new customer record into the database.
2. If that new record already exist, then display a warning message for the user stating:

"Customer already exist in system".

3. Based on 1 and 2 above, my end result should be one of the following:

Sucessfully insert new customer record. Then display a sucess message:

"The customer has been added!"

Or

Display a warning message about the customer already being in the database:

"Customer already exist in system!"



I hope this is clear.

Thanks everyone for your help!
0
Comment
Question by:Rafiq24
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:BurntSky
ID: 13882959
Are you using stored procedures?  or plain sql command queries?  What field are you using as a unique identifier?  username, email address, etc?

If you're using SQL Server, s'procs are obviously the best solution for checking for an existing record, but if you're using an Access database or some other database where s'procs arent available, it's still doable with regular sql queries.
0
 

Author Comment

by:Rafiq24
ID: 13883023
1. No Im not using store procedures.
2. Yes Im using Plain sql queries.
3. Im using an autonumber for my unique identfier
4. Im using Access 2003


How can this be done with regular sql queries?
0
 
LVL 9

Accepted Solution

by:
BurntSky earned 1000 total points
ID: 13883046
When I said "unique identifier" i meant how are you deciding if the user is already in the database?  I didn't mean the primary key.  Sorry if i was unclear.  Assuming you're using an email address, do the following:

execute this SQL command:

SELECT UserID FROM tblUsers WHERE emailaddress = '<insert email address here>'

Check the number of records returned.  If you're using a DataTable just do:

if(myDataTable.Rows.Count > 0)
    { // user already exists }
else
    { // execute INSERT command }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Rafiq24
ID: 13883068
Do i need to specify to 2  OleDBCommand variables to hold both queries. One for the "Select Query and the other for the "INSERT" Query??
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13883090
nope, you can write over top of the "select" command and re-execute it.
0
 

Author Comment

by:Rafiq24
ID: 13883093
Cool. This is what Im looking for! Thanks a million.
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13883112
no problem.  glad i could help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question