0

I have a column in type float in data base. I need to pass a float value taken from a text box to a sql quarry to insert that value to the table in data base. However the conversion,

Convert.ToSingle(textbox1.Text)

is not working.

I have tried float num = float.Parse(textBox1.Text); also. in both ways a type conversion error is shown.

And in microsoft sql server management 2014 the data type double is not available.

Any help would be highly appreciated.

code: private void button5_Click(object sender, EventArgs e) {

        if (conn.State.ToString() == "Closed")
        {
            conn.Open();
        }


        SqlCommand newCmd = conn.CreateCommand();
        newCmd.Connection = conn;
        newCmd.CommandType = CommandType.Text;
        newCmd.CommandText = "insert into salesTable(cusName,cusId,drugName,drugId,strength,quantity,unitPrice,receiptNo) values('" + Convert.ToString(metroTextBox1.Text) + "','" + Convert.ToInt32(metroTextBox2.Text) + "','" + Convert.ToString(metroComboBox1.SelectedItem) + "','" + Convert.ToInt32(metroTextBox4.Text) + "','" + Convert.ToString(metroTextBox5.Text) + "','" + Convert.ToInt32(metroTextBox6.Text) + "','" + Convert.ToSingle(metroTextBox7.Text) + "','" + Convert.ToInt32(metroTextBox8.Text) + "')";
        newCmd.ExecuteNonQuery();


        conn.Close();
        ClearAllText(this);
        comboFill();
        MessageBox.Show("sale has been recorded.");
    }
angels65
  • 57
  • 1
  • 6
  • Can you add your code to show what you have tried. – Karl Gjertsen Sep 18 '16 at 10:01
  • 4
    It may be nice to know your locale settings and your input string. – Adriano Repetti Sep 18 '16 at 10:02
  • Read [ask]. "Not working" is not a proper problem description. – CodeCaster Sep 18 '16 at 10:04
  • Few notes: Convert.ToString() is useless, metroTextBox.Text is already a string. You're literally adding typed numbers into SQL code, it's a security risk (because of SQL injection) and also wrong because user will type according to its locale rules (not according to SQL rules, think about decimal separator) – Adriano Repetti Sep 21 '16 at 08:33

0 Answers0