none
com.ExecuteNonQuery() function not inserting data and no error message. RRS feed

  • Question

  • Hi, From a Windows desktop application I try to insert a row into MS Access database table using OleDb Command with ExecuteNonQuery(). Code executing normally with out errors, but data not inserted to database. Here is the code.

    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    OleDbCommand com = new OleDbCommand("Insert into Devo (DID,Dname,Address,Phone) values (@id,@name,@address,@phone)", conn);
                    com.Parameters.AddWithValue("@id", lblCnt.Text);
                    com.Parameters.AddWithValue("@name", txtName.Text);
                    com.Parameters.AddWithValue("@address", txtAddress.Text);
                    com.Parameters.AddWithValue("@phone", txtPhone.Text);
                    conn.Open();
                    int x = com.ExecuteNonQuery();
                    MessageBox.Show("New Person info has been saved."+x.ToString(), "Saved", MessageBoxButtons.OK);
                    txtPhone.Text = "";
                    txtAddress.Text = "";
                    txtName.Text = "";
                    txtGotram.Text = "";
                    lblCnt.Text = (Convert.ToInt32(lblCnt.Text)+1).ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                finally
                {
                    conn.Close();
                }
            }



    K.R.V.Prasad Sarma


    Friday, October 30, 2015 3:21 AM

Answers

  • Hi Edward, Thanks for reply. I changed location of Database file to D: Drive. The same code works perfect. I think we need to give write permission to application folders. I removed read-only property to the folder. But still no result. Finally moving database file out from the application directory helps me. 

    K.R.V.Prasad Sarma


    Friday, November 6, 2015 7:58 AM

All replies

  • Hello,

    From the above code snippet, I think it's Ok.  What I can think is to examine the connection , Can you use the following code to check that you are using the correct OleDb Connection?

    OleDbConnection conn = new OleDbConnection("");
                conn.Open();
                string state = conn.State.ToString();
                conn.Close();

    If the above is not helpful, please send me your project which has been removed those unnecessary code. Use your OneDrive and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 2, 2015 5:11 AM
  • Hi Herro, I checked that and the same connection is working fine to retrive data and fill the data grid. But its not responding for Insert command. 

    K.R.V.Prasad Sarma

    Wednesday, November 4, 2015 7:06 AM
  • Hi K.R.V.Prasad,

    Based on your description, your connection was ok, and I assume it might be related with the insert statement, I suggest you output the “com”, and copy it to Access database to check whether it could work correctly.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, November 5, 2015 9:40 AM
  • Hi Edward, Thanks for reply. I changed location of Database file to D: Drive. The same code works perfect. I think we need to give write permission to application folders. I removed read-only property to the folder. But still no result. Finally moving database file out from the application directory helps me. 

    K.R.V.Prasad Sarma


    Friday, November 6, 2015 7:58 AM
  • Hi Herro Wong, Thanks for reply. I changed location of Database file to D: Drive. The same code works perfect. I think we need to give write permission to application folders. I removed read-only property to the folder. But still no result. Finally moving database file out from the application directory helps me. 

    K.R.V.Prasad Sarma

    Friday, November 6, 2015 7:59 AM
  • Hi K.R.V.Prasad,

    I suggest you check the read-only property of the Access database file.

    If you manually open the database, could you edit the records?

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, November 7, 2015 4:12 AM