rtay
asked on
VB.Net Winform how to check dataset for Duplicate and return error to user if there is one
I have a windows form connected to a dataset. I have several fields that can not be duplicates, such as truck number and truck license plate. The database is aleady set not to accept duplicates. I want the application to display an error if they try to enter a duplicate. Right now the application just crashes if I try to enter a duplicate. It saves to the database correctly if I do not enter a duplicate. Thank you.
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Text.RegularExpressions
Imports System.Data.SqlClient
Imports System.DBNull
Imports System.Data.Sql
Imports System.ComponentModel.CancelEventArgs
Imports System.ComponentModel
Imports System.Windows.Forms.ErrorProvider
Imports System.Data
Imports System.Console
Public Class frmTruckInfo
' for printing form
Inherits Form
Private WithEvents printDocument1 As New PrintDocument
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Controls.Add(printButton)
End Sub
Dim memoryImage As Bitmap
Private Sub CaptureSceen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
End Sub
Private Sub printDocument1_PrintPage(ByVal Sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
printDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub 'end print screen
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
End
End Sub
Private Sub TrkLicenseLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub FindToolStrip_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs)
End Sub
Private Sub YoungsTractorsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YoungsTractorsToolStripMenuItem.Click
Dim frmYngTrk As New frmYoungsTractor
frmYngTrk.Show()
Me.Close()
End Sub
Private Sub Tbl_TruckInfoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.Tbl_TruckInfoBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Develop_TareWeightDataSetTruckInfoAdd)
End Sub
Private Sub frmTruckInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Develop_TareWeightDataSetTruckInfoAdd.tbl_TruckInfo' table. You can move, or remove it, as needed.
Me.Tbl_TruckInfoTableAdapter.Fill(Me.Develop_TareWeightDataSetTruckInfoAdd.tbl_TruckInfo)
'TODO: This line of code loads data into the 'Develop_TareWeightDataSetTruckInfoAdd.tbl_TruckInfo' table. You can move, or remove it, as needed.
Me.Tbl_TruckInfoTableAdapter.Fill(Me.Develop_TareWeightDataSetTruckInfoAdd.tbl_TruckInfo)
End Sub
Private Sub FillByToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Me.Tbl_TruckInfoTableAdapter.FillBy(Me.Develop_TareWeightDataSetTruckInfoAdd.tbl_TruckInfo, TruckNoToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
' print screen button
Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
CaptureSceen()
printDocument1.Print()
End Sub
Private Sub Tbl_TruckInfoBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tbl_TruckInfoBindingNavigatorSaveItem.Click
Me.Validate()
Me.Tbl_TruckInfoBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Develop_TareWeightDataSetTruckInfoAdd)
If Develop_TareWeightDataSetTruckInfoAdd.HasErrors = True Then
MsgBox("Error ")
Exit Sub
End If
End Sub
Private Sub FillBy1ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FillBy1ToolStripButton.Click
Try
Me.Tbl_TruckInfoTableAdapter.FillBy1(Me.Develop_TareWeightDataSetTruckInfoAdd.tbl_TruckInfo, TruckNoToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TruckNoTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TruckNoTextBox.Validating
Dim Regex As New Regex("^[Y,S,0-9]{1,4}$")
If Regex.IsMatch(TruckNoTextBox.Text) Then
ErrorProvider2.SetError(TruckNoTextBox, "Please enter correct format")
Else
ErrorProvider2.SetError(TruckNoTextBox, "Wrong")
End If
End Sub
Public ReadOnly Property HasErrorrs() As Boolean
Get
End Get
End Property
Private Sub CheckForErrors()
If Not Develop_TareWeightDataSetTruckInfoAdd.HasErrors Then
Develop_TareWeightDataSetTruckInfoAdd.Merge(Develop_TareWeightDataSetTruckInfoAdd)
Else
PrintRowErrs(Develop_TareWeightDataSetTruckInfoAdd)
End If
End Sub
Private Sub PrintRowErrs(ByVal dataSet As DataSet)
Dim row As DataRow
Dim table As DataTable
For Each table In dataSet.Tables
For Each row In table.Rows
If row.HasErrors Then
Console.WriteLine(row.RowError)
End If
Next
Next
End Sub
End Class
I believe this can be accomplished by handling DataGridView1.DataError. Within this method you can use the DataGridViewDataErrorEvent Args object to access the exception and determine how to handle it.
ASKER
Can you give a quick example of that syntax?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.