Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

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

Open in new window

0
rtay
Asked:
rtay
  • 2
1 Solution
 
Corey ScheichDeveloperCommented:
I believe this can be accomplished by handling DataGridView1.DataError.  Within this method you can use the DataGridViewDataErrorEventArgs object to access the exception and determine how to handle it.
0
 
rtayAuthor Commented:
Can you give a quick example of that syntax?
0
 
Corey ScheichDeveloperCommented:
I'm sorry I thought you were using a datagrid view.  This thread is exactly what you need

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/6a10a9c2-edd0-49be-b4ee-a9f065a90174
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now