kwangu

Prevent duplicate data in visual basic

Oct 15th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub insert_category()
  2.         Call connection_string()
  3.         MysqlConn.Open()
  4.         Query = "SELECT * FROM tbl_categories WHERE CategoryName = @CategoryName"
  5.         COMMAND = New MySqlCommand(Query, MysqlConn)
  6.         COMMAND.Parameters.AddWithValue("@CategoryName", txtCategoryName.Text)
  7.         READER = COMMAND.ExecuteReader
  8.         If READER.HasRows Then
  9.             'category already exists
  10.            MsgBox(txtCategoryName.Text & " " & "already exist.Enter another name and try again", MsgBoxStyle.Exclamation, "Product")
  11.             MysqlConn.Close()
  12.         Else
  13.             Try
  14.                 Call connection_string()
  15.                 MysqlConn.Open()
  16.                 Query = "INSERT INTO tbl_categories (CategoryName) VALUES (@CategoryCode,@CategoryName,@Datecreated,@status)"
  17.                 COMMAND = New MySqlCommand(Query, MysqlConn)
  18.                 COMMAND.Parameters.AddWithValue("@CategoryName", txtCategoryName.Text)
  19.                 READER = COMMAND.ExecuteReader
  20.                 MysqlConn.Close()
  21.                 MsgBox(txtCategoryName.Text & " " & "added successfully!", MsgBoxStyle.Information, "Success")
  22.                 txtCategoryName.Text = ""
  23.                 txtCategoryName.Focus()
  24.             Catch ex As Exception
  25.                 MsgBox(ex.Message)
  26.             End Try
  27.         End If
  28.     End Sub
Add Comment
Please, Sign In to add comment