1
votes

Now i get the problem at the try statement ( int count =insert.......). The compailer says(when i push the buton on the form to save the values in textboxes) that it Failed to convert parameter value from a String to a Int32. code:

private void button1_Click(object sender, EventArgs e) {

       string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
     + "Data Source=C:\\Users\\Simon\\Desktop\\save.mdb";

        OleDbConnection empConnection = new OleDbConnection(conString);


        string insertStatement = "INSERT INTO obroki_save "
                             + "([ID_uporabnika],[datum],[ID_zivila],[skupaj_kalorij]) "
                             + "VALUES (@ID_uporabnika,@datum,@ID_zivila,@skupaj_kalorij)";

        OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);

        insertCommand.Parameters.Add("@ID_uporabnika", OleDbType.Integer).Value = users.iDTextBox.Text;
        insertCommand.Parameters.Add("@datum", OleDbType.Date).Value = DateTime.Now;
        insertCommand.Parameters.Add("@ID_zivila", OleDbType.Integer).Value = iDTextBox.Text;
        insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Integer).Value = textBox1.Text;
        empConnection.Open();

        try
        {
           int count = insertCommand.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            empConnection.Close();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
        }
    }
2

2 Answers

1
votes
insertCommand.Parameters.Add("@datum", OleDbType.Char).Value = DateTime.Now;

You're inserting a date as datatype Char, which looks wrong.

0
votes

You need to convert your data types: textbox.value hold a string and to convert it, you can use int.Parse.

    insertCommand.Parameters.Add("@ID_zivila", OleDbType.Integer).Value = int.Parse(iDTextBox.Text);
    insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Integer).Value = int.Parse(textBox1.Text);