locked
How to convert a varchar to float? RRS feed

  • Question

  • It gives me this Error "Error converting data type varchar to float." 

    when it gets this code:

    com.CommandText = "DELETE FROM tabel1 WHERE abc='" + textBox1.Text + "';"; com.ExecuteNonQuery();

    The text of the textBox1 is "12,5" and the colum abc form database is a float column.

    Please help!

    • Moved by Amanda Zhu Wednesday, July 24, 2013 5:55 AM not VS general issues
    Monday, July 22, 2013 12:54 PM

Answers

  • You should use parameters to get the syntax correct as your code is formatting the text as a string.

    Granted the example below is VB but the same holds true for any .NET language. If the text is Double then create the param as Double, if Int32 then create it as Int32

        Private Sub Example()
            Dim Value As Double = 0
            If Double.TryParse(Textbox1.Text, Value) Then
                Dim com As New OleDb.OleDbCommand With {.CommandText = "DELETE FROM tabel1 WHERE abc=@P1"}
                com.Parameters.Add(New OleDbParameter With {.ParameterName = "@P1", .DbType = DbType.Double, .Value = Value})
                com.ExecuteNonQuery()
            End If
        End Sub



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, July 22, 2013 1:21 PM
  • The code below will take a Hex string (8 characters) and convert to to a float (32 bits).  If you have an integer already, just eliminate the code that converts the string to a Uint64.  So your input would be bigendian.

     double fractionDivide = Math.Pow(2, 23);
                string littleendian = Hex32InputBox.Text;
                string bigendianstr = "";
                double doubleout;
                ////reverse bytes
                //for (int i = 0; i < littleendian.Length; i += 2)
                //{
                //    bigendianstr = littleendian.Substring(i, 2) + bigendianstr;
                //}
                bigendianstr = littleendian;
                UInt64 bigendian = (UInt64)tohex(bigendianstr);
                int sign = (bigendian & 0x80000000) == 0 ? 1 : -1;
                Int64 exponent = ((Int64)(bigendian & 0x7F800000) >> 23) - (Int64)127;
                UInt64 fraction = (bigendian & 0x007FFFFF);
                double temp = (double)fraction / fractionDivide;
                if (fraction == 0)
                    doubleout = sign * Math.Pow(2, exponent);
                else
                    doubleout = sign * (1 + (fraction / fractionDivide)) * Math.Pow(2, exponent);
                string doublestr = doubleout.ToString();
                float32OutputBox.Text = doublestr;

    Tohex function

    Int64 tohex(string inp)
            {
                Int64 outp = 0;
                char nibblechar = '\0';
                byte nibble = 0;
                for(int pos = 0; pos< inp.Length; pos++)
                {
                    nibblechar = inp.Substring(pos, 1).ToUpper().ToCharArray()[0];
                    nibble = (byte)nibblechar;
                    switch (nibblechar)
                    {
                        case 'A' :
                        case 'B' :
                        case 'C' :
                        case 'D' :
                        case 'E' :
                        case 'F' :
                            nibble = (byte)nibblechar;
                            nibble = (byte)(nibble - (byte)'A');
                            nibble = (byte)(nibble + 10);
                            break;
                        case '0' :
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            nibble = (byte)(nibble - (byte)'0');
                            break;
                    }
                    outp = (outp << 4) + nibble;
        
                }
                return (outp);
            }


    jdweng

    Monday, July 22, 2013 2:40 PM

All replies

  • You should use parameters to get the syntax correct as your code is formatting the text as a string.

    Granted the example below is VB but the same holds true for any .NET language. If the text is Double then create the param as Double, if Int32 then create it as Int32

        Private Sub Example()
            Dim Value As Double = 0
            If Double.TryParse(Textbox1.Text, Value) Then
                Dim com As New OleDb.OleDbCommand With {.CommandText = "DELETE FROM tabel1 WHERE abc=@P1"}
                com.Parameters.Add(New OleDbParameter With {.ParameterName = "@P1", .DbType = DbType.Double, .Value = Value})
                com.ExecuteNonQuery()
            End If
        End Sub



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, July 22, 2013 1:21 PM
  • The code below will take a Hex string (8 characters) and convert to to a float (32 bits).  If you have an integer already, just eliminate the code that converts the string to a Uint64.  So your input would be bigendian.

     double fractionDivide = Math.Pow(2, 23);
                string littleendian = Hex32InputBox.Text;
                string bigendianstr = "";
                double doubleout;
                ////reverse bytes
                //for (int i = 0; i < littleendian.Length; i += 2)
                //{
                //    bigendianstr = littleendian.Substring(i, 2) + bigendianstr;
                //}
                bigendianstr = littleendian;
                UInt64 bigendian = (UInt64)tohex(bigendianstr);
                int sign = (bigendian & 0x80000000) == 0 ? 1 : -1;
                Int64 exponent = ((Int64)(bigendian & 0x7F800000) >> 23) - (Int64)127;
                UInt64 fraction = (bigendian & 0x007FFFFF);
                double temp = (double)fraction / fractionDivide;
                if (fraction == 0)
                    doubleout = sign * Math.Pow(2, exponent);
                else
                    doubleout = sign * (1 + (fraction / fractionDivide)) * Math.Pow(2, exponent);
                string doublestr = doubleout.ToString();
                float32OutputBox.Text = doublestr;

    Tohex function

    Int64 tohex(string inp)
            {
                Int64 outp = 0;
                char nibblechar = '\0';
                byte nibble = 0;
                for(int pos = 0; pos< inp.Length; pos++)
                {
                    nibblechar = inp.Substring(pos, 1).ToUpper().ToCharArray()[0];
                    nibble = (byte)nibblechar;
                    switch (nibblechar)
                    {
                        case 'A' :
                        case 'B' :
                        case 'C' :
                        case 'D' :
                        case 'E' :
                        case 'F' :
                            nibble = (byte)nibblechar;
                            nibble = (byte)(nibble - (byte)'A');
                            nibble = (byte)(nibble + 10);
                            break;
                        case '0' :
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            nibble = (byte)(nibble - (byte)'0');
                            break;
                    }
                    outp = (outp << 4) + nibble;
        
                }
                return (outp);
            }


    jdweng

    Monday, July 22, 2013 2:40 PM