Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
428 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:Corey Scheich
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:
Corey Scheich earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

636 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