By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,775 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,775 IT Pros & Developers. It's quick & easy.

Insert via C# / Ado.net

100+
P: 147
I have a web page which a user can insert their information (First Name, Last Name and Email) which works well. It writes this to an Access database.

All I want to do is prevent the user entering an email address which already exists! This would then ensure none of the fields were updated. (eg First Name, Last Name And Email). Basically, how do I avoid creating duplicates in the table!

Thanks

Dave
Jan 21 '09 #1
Share this Question
Share on Google+
8 Replies


RedSon
Expert 5K+
P: 5,000
Select the rows that match the email address, if you return > 0 then it already exists, and you can ignore it.
Jan 21 '09 #2

Plater
Expert 5K+
P: 7,872
Set the Email field in the table to be unique? the ODBC driver will throw an exeption if a duplicate is attempted to insert
Jan 21 '09 #3

100+
P: 147
Thank you both for your replies. I set the table up to have a unique field (indexed according to Access) and this works by throwing the error which I can catch.

Red Son, how do match a row? I've worked out how to count rows, but not how to match. The code I use to match is

ds.Tables["BoardToBoard"].Rows.Count;
I would love to do ds.Tables["BoardToBoard"].Rows.["Email"] =="xxx";

Any suggestions?

Thank you again

Dave
Jan 22 '09 #4

Curtis Rutland
Expert 2.5K+
P: 3,256
I think he meant to do a select against your table and count the number of rows returned

SELECT count(*) FROM table WHERE email = @email

and if the return value is zero, the email address is unique. Otherwise it's not.

Or you could do a foreach loop in your table:
Expand|Select|Wrap|Line Numbers
  1. foreach (DataRow r in ds.Tables["BoardToBoard"])
  2. {
  3.   if(r["Email"] == newEmailAddress)
  4.     //at this point, the email is non-unique and you should set some flag or return true
  5. }
It would be better to do this on the database side though.
Jan 22 '09 #5

Plater
Expert 5K+
P: 7,872
You could also use the .Select() on the datatable.
Expand|Select|Wrap|Line Numbers
  1. DataRow[] matchingrows= ds.Tables["BoardToBoard"].Select("[email] = "+EmailToCheck);
  2. if(matchingrows.Length == 0)
  3. {//value is unique
  4. }
  5.  
Jan 22 '09 #6

Curtis Rutland
Expert 2.5K+
P: 3,256
Good point Plater, I forgot about that. Which do you think would be more efficient? Passing it off to the OleDb engine or doing it in memory from your program?
Jan 22 '09 #7

100+
P: 147
Again, thank you everyone for your helpful suggestions. This has answered many questions I had as well as my main question. Thank you
Jan 22 '09 #8

RedSon
Expert 5K+
P: 5,000
@insertAlias
I vote for passing it to OleDb.
Jan 22 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.