473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

vb.net/c# : Check duplicate record before inserting in database

Hi,
I want to check in the below code if the email exists in the database before inserting. How can I do that?

Expand|Select|Wrap|Line Numbers
  1. Dim EmailAlertDataSource As New SqlDataSource()
  2.         EmailAlertDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("DB_EmailAlertConnectionString1").ToString()
  3.  
  4.         EmailAlertDataSource.InsertCommandType = SqlDataSourceCommandType.Text
  5.         EmailAlertDataSource.InsertCommand = "INSERT INTO Table_Emails (EmailAccount, EmailAccountDateEntered, EmailCategory) VALUES (@EmailAccount, @EmailAccountDateEntered, @EmailCategory)"
  6.         EmailAlertDataSource.InsertParameters.Add("EmailAccount", tbemail.Text) 'HOW TO CHECK THIS EMAIL EXISTS IN DATABASE??
  7.         EmailAlertDataSource.InsertParameters.Add("EmailAccountDateEntered", DateTime.Now.ToString("f", New CultureInfo("en-GB")))
  8.  
  9.         Dim MyItem As ListItem
  10.         Dim catSelected As String = ""
  11.         Dim catSelected1 As String = ""
  12.         'Dim r As Integer
  13.         For Each MyItem In CheckBoxList1.Items
  14.             If MyItem.Selected = True Then
  15.                 catSelected = catSelected & MyItem.Text & ","
  16.                 Dim i As Integer = Len(catSelected)
  17.                 catSelected1 = Mid(catSelected, 1, i - 1)
  18.                 'lblMessage.Text = catSelected1
  19.             End If
  20.         Next
  21.  
  22.         EmailAlertDataSource.InsertParameters.Add("EmailCategory", catSelected1)
  23.  
  24.         Dim rowsAffected As Integer = 0
  25.  
  26.         Try
  27.  
  28.             rowsAffected = EmailAlertDataSource.Insert()
  29.  
  30.         Catch ex As Exception
  31.  
  32.  
  33.             'Response.Write("<p>strTime:" & strTime)
  34.             lblError.Text = "An error has occured/"
  35.             lblError.Text += ex.Message()
  36.  
  37.  
  38.             'TODO: To create this page
  39.             'Server.Transfer("email_problem_new.aspx")
  40.  
  41.         Finally
  42.  
  43.             EmailAlertDataSource = Nothing
  44.  
  45.         End Try
  46.  
  47.         If rowsAffected <> 1 Then
  48.  
  49.             'Server.Transfer("email_problem_new.aspx")
  50.  
  51.         Else
  52.  
  53.             'Server.Transfer("email_confirm_new.aspx")
  54.  
  55.         End If
  56.  
Oct 24 '07 #1
3 7402
Shashi Sadasivan
1,435 Expert 1GB
create a select command which gets the records based on the email.
If the number of records is more than zero, then the email already exists !!!
Oct 24 '07 #2
dip_developer
648 Expert 512MB
Hi,
I want to check in the below code if the email exists in the database before inserting. How can I do that?

Expand|Select|Wrap|Line Numbers
  1. Dim EmailAlertDataSource As New SqlDataSource()
  2. EmailAlertDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("DB_EmailAlertConnectionString1").ToString()
  3.  
  4. EmailAlertDataSource.InsertCommandType = SqlDataSourceCommandType.Text
  5. EmailAlertDataSource.InsertCommand = "INSERT INTO Table_Emails (EmailAccount, EmailAccountDateEntered, EmailCategory) VALUES (@EmailAccount, @EmailAccountDateEntered, @EmailCategory)"
  6. EmailAlertDataSource.InsertParameters.Add("EmailAccount", tbemail.Text) 'HOW TO CHECK THIS EMAIL EXISTS IN DATABASE??
  7. EmailAlertDataSource.InsertParameters.Add("EmailAccountDateEntered", DateTime.Now.ToString("f", New CultureInfo("en-GB")))
  8.  
  9. Dim MyItem As ListItem
  10. Dim catSelected As String = ""
  11. Dim catSelected1 As String = ""
  12. 'Dim r As Integer
  13. For Each MyItem In CheckBoxList1.Items
  14. If MyItem.Selected = True Then
  15. catSelected = catSelected & MyItem.Text & ","
  16. Dim i As Integer = Len(catSelected)
  17. catSelected1 = Mid(catSelected, 1, i - 1)
  18. 'lblMessage.Text = catSelected1
  19. End If
  20. Next
  21.  
  22. EmailAlertDataSource.InsertParameters.Add("EmailCategory", catSelected1)
  23.  
  24. Dim rowsAffected As Integer = 0
  25.  
  26. Try
  27.  
  28. rowsAffected = EmailAlertDataSource.Insert()
  29.  
  30. Catch ex As Exception
  31.  
  32.  
  33. 'Response.Write("<p>strTime:" & strTime)
  34. lblError.Text = "An error has occured/"
  35. lblError.Text += ex.Message()
  36.  
  37.  
  38. 'TODO: To create this page
  39. 'Server.Transfer("email_problem_new.aspx")
  40.  
  41. Finally
  42.  
  43. EmailAlertDataSource = Nothing
  44.  
  45. End Try
  46.  
  47. If rowsAffected <> 1 Then
  48.  
  49. 'Server.Transfer("email_problem_new.aspx")
  50.  
  51. Else
  52.  
  53. 'Server.Transfer("email_confirm_new.aspx")
  54.  
  55. End If
  56.  
you can check for the record with IfExists keyword in SQL before inserting if you are using sql-server as your database

syntax would be like....
IfExists(SELECT email_id FROM myTable WHERE email_id=2)
Oct 25 '07 #3
kunal pawar
297 100+
Hi,
I suggest you to make that column as Primary key. So its through Exception catch tht one and do your action
Oct 25 '07 #4

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

Similar topics

1
by: g_chime | last post by:
How can I make MySQL reject duplicate rows when inserting? The table is quite large: 100+ rows and millions of rows. MySQL doesn't allow me to create a UNIQUE INDEX for all the rows (16 rows...
6
by: planetthoughtful | last post by:
Hi All, I have a C# ASP.NET page that submits back to itself to insert details from a form into a database table. When / if the user refreshes the page (and gets the standard warning that POST...
8
by: mindwarp | last post by:
Hi, When a user submits / posts data my php script Inserts data into my database. If they refresh the script or click back and click submit again I get duplicate record. Is there an easy...
4
by: FangQ | last post by:
hi I am very new to mysql. I have a question about using the "on duplicate update" clause with insert command. my table "data" has two columns, field1 and field2, where field1 is the index...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
5
by: Alan Little | last post by:
I have affiliates submitting batches of anywhere from 10 to several hundred orders. Each order in the batch must include an order ID, originated by the affiliate, which must be unique across all...
7
by: ebindia0041 | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, Asp.net 1.1 with c# I'm inserting simple records into a table. But one...
11
by: richardkreidl | last post by:
I'm trying to check for duplicates before I do an INSERT into the Access database table. I basically want to alert the user if the 'ProjectName' and the 'MileStones' are already in the table.. ...
2
by: Ranma13 | last post by:
Hello, I have a duplicate record check written in VB for a check in/check out database. Here's the pseudocode, written for the BeforeUpdate property on the form: If DCount(search for records with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.