Input string was not in a correct format error RRS feed

  • Question

  • User1717218719 posted

    I have the followig code in which I am recieveing the error "

    Input string was not in a correct format." on the line:

     textbx.Text = CDec(tmpTbl("Com").ToString).ToString("0.00")

    I am trying to set the value of a textbox on my webpage using the value in the sql datatable

    The code I have is the following:

           comComm = New SqlCommand
            With comComm
                .Connection = conConn
                .CommandType = CommandType.Text
                .CommandText = "SELECT * FROM TABLE WHERE (X = @X)"
                .Parameters.AddWithValue("@X", argparam)
             End With
            adpAdap = New SqlDataAdapter(comComm)
            adpAdap.Fill(Dst, "tbl")
            Dim tmpTbl As DataTable = argDst.Tables("tbl")
            If tmpTbl.Rows.Count > 0 Then
                txtBx.Text = CDec(tmpTbl("Com").ToString).ToString("0.00")
            End If

    Friday, July 5, 2019 1:48 PM

All replies

  • User475983607 posted

    The error is telling you that tmpTbl("Com") is not formatted a decimal and therefore the command fails.  For example, "1A2" would fail.

    Keep in mind, we cannot see the data or the table schema.  I recommend basic troubleshooting to find the value of tmpTbl("Com").  Once you know that basic bit of information you'll probably be able to solve the problem.  Use the Visual Studio debugger to step through your code.

    IMHO, it's much easier to format the result using T-SQL than C#.  Given your code I assume the main issue is the decimal in SQL has more than 2 significant figures?

    Friday, July 5, 2019 2:03 PM
  • User288213138 posted

    Hi E.RU,

    textbx.Text = CDec(tmpTbl("Com").ToString).ToString("0.00")

    tmpTbl represents System.Data.DataRow,the parameter inside should be a row in the Datatable, not a field in the database.

    This demo can be used as a reference.

    The code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr527").ConnectionString)
                Using cmd As SqlCommand = New SqlCommand("select * from Months", con)
                    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    If dt.Rows.Count > 0 Then
                        TextBox1.Text = CDec(dt(0).Item(0).ToString).ToString("0.00")
                    End If
                End Using
            End Using
        End Sub

    Best regards,


    Monday, July 8, 2019 8:22 AM