Muthu Kumar
Muthu Kumar

Reputation: 23

Insert float values to Access

This is my code, which inserts values in MS Access. But it doesn't insert float values, like 0.45. The column datatype is text

private void button1_Click(object sender, EventArgs e)
{
    OleDbCommand cmd = con.CreateCommand();
    con.Open();
    cmd.CommandText = "insert into hhh VALUES('" + textBox1.Text + "','" +textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
    cmd.Connection = con;
    cmd.ExecuteNonQuery();
    MessageBox.Show("Record Submitted", "Congrats");
    con.Close();
}

Upvotes: 0

Views: 1683

Answers (1)

Gustav
Gustav

Reputation: 55961

You insert everything as text while a float is a number. Also, ensure a proper string expression of the value, say for textBox1:

string text1;
// text1 must be formatted with dot as decimal separator.
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.InvariantCulture;
text1 = Convert.ToDouble(textBox1.Text).ToString(culture);

cmd.CommandText = "insert into hhh VALUES (" + text1 + ",'" +textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

That said, do use parameters - simpler, proven, and preferred.

Upvotes: 1

Related Questions