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
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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