Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
|
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2007, 12:36 PM
Authorized User
 
Join Date: Dec 2006
Posts: 70
Thanks: 0
Thanked 1 Time in 1 Post
Default Need help understanding validation

I can't seem to get this validation routine to work.
Can anyone give me some clues? I thought I was following the proper sequence of events, but I have apparently left something out.

Code:
    Private strCellESN As String
    Private intNetworkID As Integer
    Private Sub dgvRMA_SetDefaultValues(ByVal sender As System.Object, _
        ByVal e As DataGridViewRowEventArgs) Handles dgvRMA.DefaultValuesNeeded

        With e.Row
            'default values for new Equipment row
            .Cells(0).Value = 0                             'ESN
            .Cells(1).Value = cmbCarrier.SelectedValue      'Carrier
            .Cells(2).Value = cmbPhone.SelectedValue        'Phone
            .Cells(3).Value = cmbColor.SelectedValue        'Color
            .Cells(4).Value = cmbLocation.SelectedValue     'Location
            .Cells(5).Value = cmbStatus.SelectedValue       'Status
            .Cells(6).Value = txtDebitMemoID.Text           'DebitMemo
            .Cells(7).Value = ""                            'Invoice Nbr
            .Cells(8).Value = 0                             'RMA Cost
            .Cells(9).Value = My.User.Name                  'user_insert
            .Cells(10).Value = Now()                        'date_insert
            .Cells(11).Value = My.User.Name                 'user_update
            .Cells(12).Value = Now()                        'date_update
            .Cells(13).Value = cmbVendor.SelectedValue      'Customer
        End With
    End Sub
    Private Sub dgvRMA_CellValidating( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms. _
        DataGridViewCellValidatingEventArgs) _
        Handles dgvRMA.CellValidating

        Dim blnValidESN As Boolean

        'validate ESN
        If e.ColumnIndex = 0 Then
            blnValidESN = _
                EquipmentNumberValidation(e.ColumnIndex.ToString(), cmbCarrier.SelectedValue)
            If Not blnValidESN Then
                e.Cancel = True
            End If
        End If

    End Sub
    Private Sub dgvRMA_CellEndEdit(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
        Handles dgvRMA.CellEndEdit

        'clear the error message
        dgvRMA.Rows(e.RowIndex).ErrorText = Nothing

    End Sub
        Private Function GetNetworkType(ByRef intCarrierID As Integer) As Integer
        Me.Cursor = Cursors.WaitCursor

        ' connect to SQL Server
        Dim strConn As New SqlConnection(APC_WirelessConnectionString)
        strConn.Open()

        Dim strSQL As New SqlClient.SqlCommand( _
            "SELECT NetworkID from Carrier " & _
            "   WHERE CarrierID = " & cmbCarrier.SelectedValue & _
            ";", strConn)

        Dim sqlResults As SqlClient.SqlDataReader = strSQL.ExecuteReader()
        sqlResults.Read()

        intNetworkID = sqlResults("NetworkID")

        sqlResults.Close()
        strConn.Close()

    End Function
    Private Function EquipmentNumberValidation(ByRef strCellESN As String, _
        ByVal intCarrierID As Integer) _
        As Boolean

        Dim intStrLen As Integer = Len(strCellESN)
        Dim blnNumeric As Boolean

        Select Case intNetworkID
            Case 1              'CDMA
                Select Case intStrLen
                    Case 8      'Hex
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 16))
                    Case 11     'Decimal
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 10))
                    Case 14     'Hex
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 16))
                    Case Else
                        blnNumeric = False
                End Select

            Case 4              'GSM
                Select Case intStrLen
                    Case 10     'Hex
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 16))
                    Case 15     'Decimal
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 10))
                    Case Else
                        blnNumeric = False
                End Select

            Case Else
                blnNumeric = True
        End Select

        Return blnNumeric

    End Function
The datagridview only contains one cell visible in each row, the ESN.
I am really confused by this.

Please help!

Thanks,
Karen

 
Old July 7th, 2007, 07:15 PM
Authorized User
 
Join Date: Dec 2006
Posts: 70
Thanks: 0
Thanked 1 Time in 1 Post
Default

I found the problem ...

Code:
        'validate ESN
        If e.ColumnIndex = 0 Then
            strCellESN = e.FormattedValue
            If Not EquipmentNumberValidation() Then
                dgvRMA.Rows(e.RowIndex).ErrorText = "ESN did not pass validation"
                e.Cancel = True
            End If
        End If

    Private Function GetNetworkType(ByVal intCarrierID As Integer) As Integer
        Me.Cursor = Cursors.WaitCursor

        ' connect to SQL Server
        Dim strConn As New SqlConnection(APC_WirelessConnectionString)
        strConn.Open()

        Dim strSQL As New SqlClient.SqlCommand( _
            "SELECT NetworkID from Carrier " & _
            "   WHERE CarrierID = " & intCarrierID & ";", strConn)

        Dim sqlResults As SqlClient.SqlDataReader = strSQL.ExecuteReader()
        sqlResults.Read()
        If sqlResults.HasRows() Then
            intNetworkID = sqlResults.Item(0)       'NetworkID
        End If
        sqlResults.Close()
        strConn.Close()

        Return intNetworkID

    End Function
    Private Function EquipmentNumberValidation() _
        As Boolean

        Dim intStrLen As Integer = Len(strCellESN)
        Dim blnNumeric As Boolean

        intNetworkID = GetNetworkType(cmbCarrier.SelectedValue)

        Select Case intNetworkID
            Case 1              'CDMA
                Select Case intStrLen
                    Case 8      'Hex
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 16))
                    Case 11     'Decimal
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 10))
                    Case 14     'Hex
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 16))
                    Case Else
                        blnNumeric = False
                End Select

            Case 4              'GSM
                Select Case intStrLen
                    Case 10     'Hex
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 16))
                    Case 15     'Decimal
                        blnNumeric = IsNumeric(Convert.ToInt32(strCellESN, 10))
                    Case Else
                        blnNumeric = False
                End Select

            Case Else
                blnNumeric = True
        End Select

        Return blnNumeric

    End Function
I've posted the changes to my code, hopefully someone else will benefit.

Karen






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help understanding DataRow asp_convert ADO.NET 3 March 13th, 2007 02:39 PM
Understanding Axes Chamkaur XSLT 1 August 6th, 2006 03:09 PM
understanding Variable use imroostercogburn C++ Programming 7 July 28th, 2006 06:15 PM
Understanding Recordsets? JpaulH Access VBA 2 May 10th, 2006 07:18 PM
Understanding Session rajuru Beginning PHP 2 September 25th, 2004 05:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.