I am using a visual studio 2010 and Microsoft Sql server 2005. I want to make the MsgBox("Unable to delete. Equipment Number is not found!", vbInformation, "Error") appear whenever a user enters an incorrect Equip_No. Any help is appreciated. Thanks!
Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete.Click
If MessageBox.Show("Do you really want to delete this record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation cancelled")
Exit Sub
End If
'If Command.CommandText = "SELECT FROM dbo.inhouse_hardware_marterfile_tbl WHERE Equip_No = @Equip_No" Then
Try
Dim Command As New SqlCommand
Dim con As New SqlConnection("Server=HPC-107;Database=MRPdb;integrated security= ...")
con.Open()
Command.Connection = con
Command.CommandText = " DELETE FROM dbo.inhouse_hardware_marterfile_tbl WHERE Equip_No = '" & Equip_No.Text & "'"
Dim da As New SqlDataAdapter
da.SelectCommand = Command
da.DeleteCommand = Command
Command.ExecuteNonQuery()
msgdeleted()
con.Close()
Catch ex As Exception
End Try
'Else : MsgBox("Unable to delete. Equipment Number is not found!", vbInformation, "Error")
'End If
'Command.Parameters.AddWithValue("@Equip_No", Equip_No.Text)
Grid()
ClearTextBoxes()
End Sub
The ExecuteNonQuery method returns an integer that is the number of rows affected by the command.
In your case, if the number is zero, then you could print your message
Dim result = Command.ExecuteNonQuery()
if result = 0 then
MessageBox.Show("Unable to delete. Equipment Number is not found!")
else
MessageBox.Show("Record deleted")
End if
Said that, please take note that.
You should use a parameterized query and not a string concatenation
Dim Command As New SqlCommand
Dim con As New SqlConnection("Server=HPC-107;Database=MRPdb;integrated security= ...")
con.Open()
Command.Connection = con
Command.CommandText = "DELETE FROM dbo.inhouse_hardware_marterfile_tbl " & _
"WHERE Equip_No = @eqno"
Command.Parameters.AddWithValue("@eqno", Equip_No.Text)
Dim result = Command.ExecuteNonQuery()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments