Re: Varchar to numeric error
Wrong section. This is VB Classic (VB6 and older), and your code is .Net
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())
Re: Varchar to numeric error
Re: Varchar to numeric error
Hi,
Thanks for the reply. I am still getting that same error after trying your code suggestion.
Re: Varchar to numeric error
its looks like the number is overflow try this:
Code:
Dim sqlresult As Long
sqlresult = Long.Parse(command0.ExecuteScalar())
Re: Varchar to numeric error
Hi,
Thanks, but I am still getting the same error message.
Re: Varchar to numeric error
are you sure this "012345678" is the number that gives you the error?, did you set break point?
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
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 ?
Re: Varchar to numeric error
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
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
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.
Re: Varchar to numeric error
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
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 :thumb: )