Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27210

Error Converting data type varchar to numeric

$
0
0
Hi,

In my database, I have a table with a field called KeyfobNo its data type is numeric(18,0).

Now, I am trying to have a function to check if the keyfob entered is not numeric. This worked fine..

Code:

Private Sub txtSrchKyfb_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSrchKyfb.Validating
                If Not IsNumeric(txtSrchKyfb.Text) And txtSrchKyfb.Text <> "" Then
                        ' Cancel the event moving off of the control.
                        e.Cancel = True
                        ' Select the offending text.
                        txtSrchKyfb.Select(0, txtSrchKyfb.Text.Length)
                        ' Give the ErrorProvider the error message to display.
                        ErrorProvider.SetError(txtSrchKyfb, "Only numerical values accepted")
                End If
        End Sub

Code:

Private Sub txtSrchKyfb_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSrchKyfb.Validated
                ErrorProvider.SetError(txtSrchKyfb, "")
        End Sub

The problem is when I click the Search button and enter text in txtsrchKyfb abd CLICK SEARCH button I receive the error provider message "Only numerical values accepted". Now when I use KEYUP when I entered Text i receive an error - Error Converting data type varchar to numeric

Why?

Just for info -

Search Click Event
Code:

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

                Dim query As String
                Dim cmd As New SqlCommand
                Dim conn As SqlConnection = GetDbConnection()

                Call ClearRecords()

                If txtSrchKyfb.Text = "" Then
                        MsgBox("Please enter a keyfob number to search", MsgBoxStyle.Information, "Customer Tracker")

                Else

                        Try
                                query = "SELECT keyfobNo FROM dbo.tblKeyfob WHERE KeyfobNo ='" & _
                                                txtSrchKyfb.Text & "' "
                                cmd = New SqlCommand(query, conn)
                                cmd.ExecuteNonQuery()
                        Catch ex As Exception
                                MsgBox(ex.Message, MsgBoxStyle.Information, "Customer Tracker")
                        End Try

                        Dim saved_kyfb As String
                        saved_kyfb = cmd.ExecuteScalar()

                        If saved_kyfb = "" Then
                                MessageBox.Show("This keyfob number doesn't exist in the database", "Customer Tracker", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        End If

                        Call PopulateKeyfobDetails()

                End If

        End Sub

KeyUp event

Code:

Private Sub txtSrchKyfb_KeyUp(sender As Object, e As KeyEventArgs) Handles txtSrchKyfb.KeyUp

                Dim query As String
                Dim cmd As New SqlCommand
                Dim conn As SqlConnection = GetDbConnection()

                If e.KeyCode = Keys.Enter Then

                        Call ClearRecords()

                        If txtSrchKyfb.Text = "" Then
                                MsgBox("Please enter a keyfob number to search", MsgBoxStyle.Information, "Customer Tracker")
                        Else

                                Try
                                        query = "SELECT keyfobNo FROM dbo.tblKeyfob WHERE KeyfobNo ='" & _
                                                        txtSrchKyfb.Text & "' "
                                        cmd = New SqlCommand(query, conn)
                                        cmd.ExecuteNonQuery()
                                Catch ex As Exception
                                        MsgBox(ex.Message, MsgBoxStyle.Information, "Customer Tracker")
                                End Try

                                Dim kyfb_Saved As String
                                kyfb_Saved = cmd.ExecuteScalar()

                                If kyfb_Saved = "" Then
                                        MessageBox.Show("This keyfob number doesn't exist in the database", "Customer Tracker", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                End If

                                Call PopulateKeyfobDetails()
                        End If

                End If

        End Sub

The error is highlighted in the code below in keyup event ...

Code:

Dim kyfb_Saved As String
                                kyfb_Saved = cmd.ExecuteScalar()

Thank you

Viewing all articles
Browse latest Browse all 27210

Trending Articles