|
-
Nov 19th, 2009, 08:19 AM
#1
Thread Starter
Frenzied Member
Problem When VB save in DB
Hi to all:
This peace of code put one value in DB:
Code:
Dim con1 As New OleDb.OleDbConnection(String.Format("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & drive & IDempresa))
Dim sSQL As String
valorcolocardebito = FormatNumber(valorcolocardebito, 2)
If valorcolocardebito = 0 Then
valorcolocardebito = 0
End If
sSQL = "UPDATE balancetegeral SET numeroconta = @contaacolocar, "
sSQL = sSQL & "mensaisdebito = @valorcolocardebito "
sSQL = sSQL & "WHERE numeroconta = @contaacolocar"
Dim command As New OleDb.OleDbCommand(sSQL, con1)
With command.Parameters
.AddWithValue("@contaacolocar", contaacolocar)
If valorcolocardebito = 0 Then
.AddWithValue("@mensaisdebito", DBNull.Value)
Else
.AddWithValue("@mensaisdebito", valorcolocardebito)
End If
End With
con1.Open()
command.ExecuteNonQuery()
con1.Close()
I had run the code step by step and he do a strange,better, very strange thing...
The value puted in valorcolocardebito = 263.733,69 and when I confirm this value in DB i have 263.733,70...
More surprise is because he make this for an values and for other save very well...
This could be surealistic,but it is that I have and sincerely i don't know way this happen...
Can anyone give me an suggestion?
Thanks
Last edited by sacramento; Nov 19th, 2009 at 08:32 AM.
-
Nov 19th, 2009, 08:32 AM
#2
Re: Problem When VB save in DB
What data type is the variable valorcolocardebito?
What data type is the field it is being put in to?
-
Nov 19th, 2009, 08:36 AM
#3
Thread Starter
Frenzied Member
Re: Problem When VB save in DB
What data type is the variable valorcolocardebito?
Dim valorcolocardebito As Double
What data type is the field it is being put in to?
In Db the field are number
-
Nov 19th, 2009, 08:43 AM
#4
Re: Problem When VB save in DB
Ok, well in that case using AddWithValue is OK.
The problem is almost certainly this line:
Code:
valorcolocardebito = FormatNumber(valorcolocardebito, 2)
Assuming FormatNumber is similar to the VB6 version, it returns a String formatted in a particular way... but you then store the result in a Double, so it needs to be converted - and that can go wrong based on various factors (including Regional Settings).
Due to issues like that, you should not convert numbers to Strings unless you are displaying them, instead you should just treat them as numbers. If you want to alter the number in some way, use numeric functions and operators (such as / and * ).
-
Nov 19th, 2009, 09:44 AM
#5
Thread Starter
Frenzied Member
Re: Problem When VB save in DB
Hi:
This is very weard,in fact...do you know why in some records i have the write values,and other's the record round to the next number?
I had delete the line you are suggested and the problem remain...
Then i have put in DB type field Currency and the value appears correct...
Well if the routine save in the same way all the times,that's it's a error that something it's wrong in the code,but in this case save well in a few records and save bad in other's records, what's goin on!!!!!!
-
Nov 19th, 2009, 03:22 PM
#6
Re: Problem When VB save in DB
Can you give some examples of "right" and "wrong" values?
You said the DB field was a Number, but that is not the actual data type - the real data type (which may be Integer/Double/...) is listed in one of the properties.
If you had an Integer based one, it would only save the whole numbers.
-
Nov 19th, 2009, 06:17 PM
#7
Thread Starter
Frenzied Member
Re: Problem When VB save in DB
HI:
Right Values - the code save in DB 263.733,69
Wrong values - the code save in DB 263.733,70
Like I said...sometimes the code save the right values and other times wrong values...do you believe in this!!!!!!!!
Thanks
-
Nov 20th, 2009, 06:16 AM
#8
Re: Problem When VB save in DB
That level of rounding is not surprising if you are using a floating-point data type (such as Double or Single). Floating-point numbers are designed for a high level of speed, at the expense of a small loss of accuracy.
If you want full accuracy, Currency (which is stored as a scaled integer) is the kind of thing you should be using.
-
Nov 20th, 2009, 12:37 PM
#9
Thread Starter
Frenzied Member
Re: Problem When VB save in DB
Hi:
That's that i had do...I had change for currency in DB
Thanks a lot
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
|