Solved

VB.Net Winform how to check dataset for Duplicate and return error to user if there is one

Posted on 2011-03-03
3
418 Views
Last Modified: 2012-05-11
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
Comment
Question by:rtay
  • 2
3 Comments
 
LVL 13

Expert Comment

by:Corey2
ID: 35030320
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
 
LVL 5

Author Comment

by:rtay
ID: 35030704
Can you give a quick example of that syntax?
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 500 total points
ID: 35030861
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now