none
math problem RRS feed

  • Question

  • hi members

    i read data from database and put it to a datatable

    so when i execute the calc didnt work

      Dim dd As New SqlCommand("select Codearticle,Qte,PrixUnitaire,PrixRevient,NumeroLigneCommandeFrs,NumeroEnsemble from DetailReceptionFrs where Numero='" & TextBox1.Text & "' and NumeroEnsemble='" & k & "' ", cn1)
                        Dim aa As New SqlDataAdapter(dd)
                        aa.Fill(ss)
    
                        Dim x(ss.Rows.Count) As Double
                        For i = 0 To ss.Rows.Count - 1
                            MsgBox("qte =" & ss.Rows(i).Item("Qte"))
                            Dim c = ss.Rows(i).Item("Qte")
                            Dim b = ss.Rows(i).Item("PrixUnitaire")
    
                            xt = c * b
                            MsgBox("q=" & q)
                            x(i) = ss.Rows(i).Item("Qte") * ss.Rows(i).Item("PrixUnitaire") * dat / TextBox7.Text
                            'Cours Devise
                            MsgBox(" x !!!" & x(i) & "Num Ensemble =" & k)
                            MsgBox(" xt = " & xt)
    
                        Next

    when i make

    xt=ss.Rows(i).Item("Qte") * ss.Rows(i).Item("PrixUnitaire")

    xt always equal to ss.Rows(i).Item("PrixUnitaire") and didn't take the ss.rows(i).item("Qte")

    please help


    Friday, May 24, 2019 10:59 AM

Answers

  • can verify that line of code work perfectly

    because in line work in other not thats strange for me

    here work

    x(i) = ss.Rows(i).Item("Qte") * ss.Rows(i).Item("PrixUnitaire") * dat / TextBox7.Text

    here not

    xt += CDbl(ss.Rows(i).Field(Of String)("Qte")) * CDbl(ss.Rows(i).Field(Of String)("PrixUnitaire"))

    By "here not", you have to give me more than "here not". For example, when using 

    CDbl(ss.Rows(i).Field(Of String)("Qte"))

    If the field value can not be converted to double you can check this via

    Dim someDoubleValue As Double

    If Double.TryParse(ss.Rows(i).Field(Of String)("Qte"), someDouble)

    If the above is true then use someDouble, if not the value parsed is not valid for type Double.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Houssem12 Saturday, May 25, 2019 10:23 AM
    Friday, May 24, 2019 1:52 PM
    Moderator

All replies

  • Hello,

    When writing code as done here you are at the mercy of the runtime compiler in that for instance 

    dat / TextBox7.Text

    is using the compiler to figure out data types which can and many times leads to issues. So you should be using 

    Option Strict On and to start Option Infer On

    Then you could should change to something like this were you are in control of what happens because things are strongly typed.

    Not saying this will give you the proper results (because there are unknowns to me) but saying this is how you need to code.

    '
    ' You need to use parameters anytime you are doing a dynamic where condition
    ' rather than string concatenation
    '
    Dim dd As New SqlCommand("select Codearticle,Qte,PrixUnitaire,PrixRevient," &
                             "NumeroLigneCommandeFrs,NumeroEnsemble from DetailReceptionFrs " &
                             "where Numero = @Numero and NumeroEnsemble=@NumeroEnsemble ", cn1)
    
    dd.Parameters.AddWithValue("@Numero", TextBox1.Text)
    dd.Parameters.AddWithValue("@NumeroEnsemble", k)
    Dim aa As New SqlDataAdapter(dd)
    aa.Fill(ss)
    
    Dim x(ss.Rows.Count) As Double
    For i = 0 To ss.Rows.Count - 1
        MsgBox("qte =" & ss.Rows(i).Field(Of Integer)("Qte").ToString())
        Dim c = ss.Rows(i).Field(Of Integer)("Qte")
        Dim b = ss.Rows(i).Field(Of Integer)("PrixUnitaire")
    
        xt = c * b
    
        MsgBox("q=" & q)
    
        x(i) = ss.Rows(i).Field(Of Integer)("Qte") *
               ss.Rows(i).Field(Of Integer)("PrixUnitaire") * dat / CInt(TextBox7.Text)
    
        'Cours Devise
    
        MsgBox(" x !!!" & x(i) & "Num Ensemble =" & k)
        MsgBox(" xt = " & xt)
    
    Next


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 24, 2019 11:21 AM
    Moderator
  • thank you a lot and i have error please help

    'Le cast spécifié n'est pas valide.'

    Friday, May 24, 2019 12:33 PM
  • What type of field is "Qte", if not a Integer is it a decimal etc. If a string then it need to be converted to a number and I can tell you how but first need to know the type for "Qte".

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 24, 2019 12:41 PM
    Moderator
  • type of Qte is varchar

    and i can use it as integer or double

    also if you look at x(i)

    x(i) = ss.Rows(i).Item("Qte") * ss.Rows(i).Item("PrixUnitaire") * dat / TextBox7.Text

    i use the field Qte without problem

    • Edited by Houssem12 Friday, May 24, 2019 12:58 PM
    Friday, May 24, 2019 12:49 PM
  • type of Qte is varchar

    and i can use it as integer or double

    also if you look at x(i)

    x(i) = ss.Rows(i).Item("Qte") * ss.Rows(i).Item("PrixUnitaire") * dat / TextBox7.Text

    i use the field Qte without problem

    Just because is appears to work as mentioned in my first reply the runtime engine is guessing at the data type. You can cast as per below.

    CInt(ss.Rows(i).Field(Of String)("Qte"))
    CDbl(ss.Rows(i).Field(Of String)("Qte"))

    Also, if the field is string and you are doing math then it should be a number field not a string field.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 24, 2019 1:12 PM
    Moderator
  • didn't work please help
    Friday, May 24, 2019 1:21 PM
  • can verify that line of code work perfectly

    because in line work in other not thats strange for me

    here work

    x(i) = ss.Rows(i).Item("Qte") * ss.Rows(i).Item("PrixUnitaire") * dat / TextBox7.Text

    here not

    xt += CDbl(ss.Rows(i).Field(Of String)("Qte")) * CDbl(ss.Rows(i).Field(Of String)("PrixUnitaire"))

    Friday, May 24, 2019 1:31 PM
  • can verify that line of code work perfectly

    because in line work in other not thats strange for me

    here work

    x(i) = ss.Rows(i).Item("Qte") * ss.Rows(i).Item("PrixUnitaire") * dat / TextBox7.Text

    here not

    xt += CDbl(ss.Rows(i).Field(Of String)("Qte")) * CDbl(ss.Rows(i).Field(Of String)("PrixUnitaire"))

    By "here not", you have to give me more than "here not". For example, when using 

    CDbl(ss.Rows(i).Field(Of String)("Qte"))

    If the field value can not be converted to double you can check this via

    Dim someDoubleValue As Double

    If Double.TryParse(ss.Rows(i).Field(Of String)("Qte"), someDouble)

    If the above is true then use someDouble, if not the value parsed is not valid for type Double.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Houssem12 Saturday, May 25, 2019 10:23 AM
    Friday, May 24, 2019 1:52 PM
    Moderator