VB.NET과 아래의 코드를 버튼 클릭 이벤트에서 사용하고 있습니다.vb.net을 사용하여 테이블에 같은 레코드를 삽입하는 것을 중지하는 방법
Protected Sub ibtnSendInvites_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnSendInvites.Click
Try
Dim emailList As New List(Of String)
Dim conString As String = WebConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
Dim con As New SqlConnection(conString)
con.Open()
Dim cmd As SqlCommand
For Each curRow As GridViewRow In GridView1.Rows
Dim chkSelect As CheckBox = CType(curRow.Cells(1).FindControl("chkSelect"), CheckBox)
Dim emailLabel As Label = CType(curRow.Cells(1).FindControl("lblEmailAddress"), Label)
If chkSelect.Checked Then
emailList.Add(emailLabel.Text)
cmd = New SqlCommand("uspInsertDelegate", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@CourseID", SqlDbType.Int).Value = Session("CourseID")
cmd.Parameters.Add("@CPUserID", SqlDbType.Int).Value = CType(curRow.Cells(1).FindControl("lblCPUserID"), Label).Text
cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = 25
cmd.Parameters.Add("@CreateUser", SqlDbType.VarChar).Value = Session("LoggedInUser")
cmd.ExecuteNonQuery()
End If
Next
For Each email As String In emailList
Dim message As String = "Please confirm your booking "
Dim subject As String = "UserPoint Course Booking Invitation Email"
Dim from As String = "[email protected]"
SendEmail.SendMessage(subject, message, from, email, "")
Next
Catch ex As Exception
End Try
End Sub
나는 사용자가 및 CPUserID 같은 기록을 가진 같은 CourseID를 삽입하려고하면 예외를 throw합니다.
이 질문을 확인하십시오 : [레코드가 이미있는 경우 테이블에 삽입 중지] (http://stackoverflow.com/questions/1388235/stop-inserting-in-table-if-record-already-exists) – Shoban