我已經編寫代碼在VB中使用inputbox(通過用戶選擇)從數據庫中刪除表記錄,但我有一個問題,當用戶插入錯誤值仍然顯示「記錄已成功刪除」MessageBox!如果在Visual Basic中的其他條件刪除記錄從SQL Server如果記錄存在
如何設置「if條件」來顯示記錄不存在,而不是顯示成功的刪除消息?
對不起,這是我的第一篇文章,這就是爲什麼它很長! :P
下面是代碼:
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
Try
Dim isbn As Long = InputBox("Enter Book ISBN", "Delete")
'First will delete the dependant record from published_by, book_return, memberbook_issue because
'it can not be deleted by applying on cascade delete or update cause it has composite primary key.
cmd = New SqlCommand("delete from published_by where isbn =" & isbn, cn)
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
cmd.ExecuteNonQuery()
cmd = New SqlCommand("delete from book_return where isbn =" & isbn, cn)
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
cmd.ExecuteNonQuery()
cmd = New SqlCommand("delete from memberbook_issue where isbn =" & isbn, cn)
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
cmd.ExecuteNonQuery()
cmd = New SqlCommand("delete from book where isbn = " & isbn, cn)
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
cmd.ExecuteNonQuery()
da = New SqlDataAdapter("select b.staff_id, b.pub_id, b.sub_code, b.isbn, b.book_name, b.author, b.price, b.rack_no, b.no_of_books, pby.vol_no, pby.pub_date from book b join published_by pby on b.isbn = pby.isbn", cn)
dt = New DataTable
da.Fill(dt)
dgvbook.DataSource = dt
MessageBox.Show("Record Successfully Deleted from current table & dependant table(s)", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Not Completed Because OF The Following Error " & "%" & ex.Message & "%", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
'......這就是爲什麼它很長'不夠長。請參閱[問] – Plutonix
如果您發佈您的代碼,我們將能夠爲您提供幫助 – Dane
我可以將它發佈在評論部分嗎? – Hazmat