|
-
Sep 20th, 2010, 05:17 PM
#1
Thread Starter
Member
Varchar to numeric error
Hi,
I have a textbox where I can enter a number. If the number exists in my sql database then it would change the label accordingly. It seems to work with any input number except for numbers starting with zero. If I input a number starting with zero(012345678) I get the error: Arithmetic Overflow error converting varchar to data type numeric. Please help. I need it to accept these kinds of numbers.
Code:
Imports System.Data.SqlClient
Public Class ValidateForm
Private Sub MineralCloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MineralCloseButton.Click
Me.Close()
End Sub
Private Sub CheckButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckButton.Click
Dim ParcelNo As String
ParcelNo = ParcelNumberTextBox.Text
Try
cnn = SQLDBConnect()
cnn.Open()
Catch ex As Exception
MessageBox.Show(Me, "SQL Connection Failed: " + ex.Message, "Communication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
errorlog(ex.ToString)
Exit Sub
End Try
Try
Dim sqlresult As Integer
Dim command0 As SqlClient.SqlCommand = New SqlClient.SqlCommand("Select count(*) from nw_sum where parcel_number =" & ParcelNo, cnn)
command0.CommandType = CommandType.Text
command0.CommandTimeout = 600
command0.ExecuteScalar()
sqlresult = command0.ExecuteScalar
If sqlresult = 0 Then
Label1.Text = "Yes"
Else
Label1.Text = "No"
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
cnn.Close()
End Sub
'Error Log Subroutine to write errors into log files
Public Sub errorlog(ByVal er)
Dim FILE_NAME As String = "C:\Program Files\Noxious Weeds Report\ErrorLogs\NOXErrorlog.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.Write(vbNewLine & System.DateTime.Now & er)
objWriter.Close()
Else
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, False)
objWriter.Write(vbNewLine & System.DateTime.Now & er)
objWriter.Close()
End If
End Sub
End Class
-
Sep 20th, 2010, 05:30 PM
#2
Re: Varchar to numeric error
Wrong section. This is VB Classic (VB6 and older), and your code is .Net
-
Sep 20th, 2010, 05:36 PM
#3
Thread Starter
Member
Varchar to numeric error
Hi,
I have a textbox where I can enter a number. If the number exists in my sql database then it would change the label accordingly. It seems to work with any input number except for numbers starting with zero. If I input a number starting with zero(012345678) I get the error: Arithmetic Overflow error converting varchar to data type numeric. Please help. I need it to accept these kinds of numbers.
Code:
Imports System.Data.SqlClient
Public Class ValidateForm
Private Sub MineralCloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MineralCloseButton.Click
Me.Close()
End Sub
Private Sub CheckButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckButton.Click
Dim ParcelNo As String
ParcelNo = ParcelNumberTextBox.Text
Try
cnn = SQLDBConnect()
cnn.Open()
Catch ex As Exception
MessageBox.Show(Me, "SQL Connection Failed: " + ex.Message, "Communication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
errorlog(ex.ToString)
Exit Sub
End Try
Try
Dim sqlresult As Integer
Dim command0 As SqlClient.SqlCommand = New SqlClient.SqlCommand("Select count(*) from nw_sum where parcel_number =" & ParcelNo, cnn)
command0.CommandType = CommandType.Text
command0.CommandTimeout = 600
command0.ExecuteScalar()
sqlresult = command0.ExecuteScalar
If sqlresult = 0 Then
Label1.Text = "Yes"
Else
Label1.Text = "No"
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
cnn.Close()
End Sub
'Error Log Subroutine to write errors into log files
Public Sub errorlog(ByVal er)
Dim FILE_NAME As String = "C:\Program Files\Noxious Weeds Report\ErrorLogs\NOXErrorlog.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.Write(vbNewLine & System.DateTime.Now & er)
objWriter.Close()
Else
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, False)
objWriter.Write(vbNewLine & System.DateTime.Now & er)
objWriter.Close()
End If
End Sub
End Class
Rate this post
-
Sep 20th, 2010, 05:43 PM
#4
Re: Varchar to numeric error
I can't see where you're trying to convert it to int. did you tried this:
Code:
sqlresult = Convert.ToInt32(command0.ExecuteScalar())
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 20th, 2010, 05:46 PM
#5
Re: Varchar to numeric error
-
Sep 20th, 2010, 05:57 PM
#6
Thread Starter
Member
Re: Varchar to numeric error
Hi,
Thanks for the reply. I am still getting that same error after trying your code suggestion.
-
Sep 20th, 2010, 06:02 PM
#7
Re: Varchar to numeric error
its looks like the number is overflow try this:
Code:
Dim sqlresult As Long
sqlresult = Long.Parse(command0.ExecuteScalar())
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 20th, 2010, 06:06 PM
#8
Thread Starter
Member
Re: Varchar to numeric error
Hi,
Thanks, but I am still getting the same error message.
-
Sep 20th, 2010, 06:09 PM
#9
Re: Varchar to numeric error
are you sure this "012345678" is the number that gives you the error?, did you set break point?
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 20th, 2010, 06:17 PM
#10
Re: Varchar to numeric error
ok i think i see where the problem, it's not within the VB code it's with your database column setting.
change the varchar column to bigger size.
i think this is the column you need to change "parcel_number" my guess is that its size is less the 9
btw, why is it defined as varchar? you want to keep the leading zeros? if it's not important change it to INT
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Sep 21st, 2010, 03:41 AM
#11
Re: Varchar to numeric error
Maybe it's because of this line:
Code:
Dim ParcelNo As String
What's the field type that you are using in your db for parcel_number ?
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 21st, 2010, 10:27 AM
#12
Thread Starter
Member
Re: Varchar to numeric error
-
Sep 21st, 2010, 11:42 AM
#13
Thread Starter
Member
Re: Varchar to numeric error
Hi,
I set my varchar column to 30, and I am still getting that same error, I did find out something strange. A number beginning with a zero only works if the number is 12 characters or more.
For example:
089470211000 works with no errors
08947021100 does not work
-
Sep 21st, 2010, 12:06 PM
#14
Re: Varchar to numeric error
Ummm.....
why are you executing the command TWICE?
Code:
command0.ExecuteScalar()
sqlresult = command0.ExecuteScalar
You should only be doing it once... and capturing the scalar result that one time.
Code:
'command0.ExecuteScalar() 'This line is useless anyways since you're not capturing the result
sqlresult = command0.ExecuteScalar()
Now... for your other problem... if the field is a string(text, varchar, what ever) then you need to treat your value as a string and not a number. The problem is that it thinks you want a num,ber so it's implicitly converting parcel_number fomr a varchar to a number and some entry in there somewhere is too big of an integer.
1) If it's text, treat it like text:
.SqlCommand("Select count(*) from nw_sum where parcel_number =" & ParcelNo, cnn)
should be
.SqlCommand("Select count(*) from nw_sum where parcel_number ='" & ParcelNo & "' ", cnn)
(note the extra tick marks
2) Use a parameter and not worry about it (this is the preferred and safe method
.SqlCommand("Select count(*) from nw_sum where parcel_number = @parecelNo", cnn)
.SqlCommand.Parameter.AddWithValue("@parecelNo", ParcelNo)
-- NOW execute the command
-tg
-
Sep 21st, 2010, 12:22 PM
#15
Junior Member
Re: Varchar to numeric error
Code:
Try
Dim sqlresult As Object
Dim command0 As SqlClient.SqlCommand = New SqlClient.SqlCommand("Select count(*) from nw_sum where parcel_number =" & ParcelNo, cnn)
command0.CommandType = CommandType.Text
command0.CommandTimeout = 600
sqlresult = command0.ExecuteScalar
If (sqlresult IsNot Nothing) Then
If (IsNumeric(sqlresult) = True) Then
If CLng(sqlresult) = 0 Then
Label1.Text = "Yes"
Else
Label1.Text = "No"
End If
End If
Else
Label1.Text = "No"
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
What about something like this? I think you are having issues due to it trying to assume a conversion from your executescalar call. Also you only need to call executescalar once for this, not twice.
Last edited by ckennedy79; Sep 21st, 2010 at 12:26 PM.
Using VB 2010/2008/2005 (Windows and ASP)
-
Sep 21st, 2010, 12:27 PM
#16
Junior Member
Re: Varchar to numeric error
Using VB 2010/2008/2005 (Windows and ASP)
-
Sep 21st, 2010, 12:33 PM
#17
Re: Varchar to numeric error
ckennedy - next time, instead of linking to a thread with no explanation like that... use the Report This Thread off the first post to report it as a duplicate... and give the link there... (I've gone ahead and taken care of that) ... all you did was confuse me for a moment as I thought you had simply linked back to this thread.
-tg
-
Sep 21st, 2010, 12:36 PM
#18
Re: Varchar to numeric error
Duplicate threads merged - please post each question (or variation of it) only once. If you think you have posted in the wrong forum, let a moderator know and we will move it.
(thanks for reporting it to us tg )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|