views:

108

answers:

1

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();
        }
    }
+1  A: 
insertCommand.Parameters.Add("@datum", OleDbType.Char).Value = DateTime.Now;

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

ho1
tnx for the answer, but still. i changed to date, dbdat other db___ and stil it shows the same error
simon
I notice that you also are not setting the length of the parameters, maybe part of the problem. Though I'd suggest that you amend your question to also show what datatype each of the fields are in the DB.
ho1
everything is long integer just datum is general date
simon
Try changing the OleDbType of the other ones to BigInt rather than Char.
ho1
i changed everything to OleDbType.integer and datum to OleDbType.date and now i get an error at try statement: Failed to convert parameter value from a String to a Int32.What should i do now ?
simon
Currently you're just sending in the Text member of the textboxes to the parameters. Convert these values to int first, you can see how here http://msdn.microsoft.com/en-us/library/bb397679.aspx
ho1
so i should convert at lines insertCommand.Parameters.Add? or at the end (i tried to convert at the end ....=Convert.ToIn32(users.iDTextBox.Text);) it stil didn't work!
simon
I'd thought something like: insertCommand.Parameters.Add("@ID_uporabnika", OleDbType.Char).Value = Convert.ToIn32(users.iDTextBox.Text));
ho1
you might want to update your question to add the updated sample, it's a bit confusing with these comments.
ho1