简体   繁体   中英

modify data in database C#

Hi guys i'm trying to modify a piece of data in my database here is the section of code i'm using.

    private void btnModifyMember_Click(object sender, EventArgs e)
    {
        string memberid = txtMemberID.Text;
        string lastname = txtLastName.Text;
        string firstname = txtFirstName.Text;
        string phone = txtPhoneNumber.Text;
        string email = txtEmail.Text;

        string update = "UPDATE [Club_Member] SET [MemberID]=@memberid,[LastName]=@lastname,[FirstName]=@firstname,[Phone]=@phone,[E_mail]=@email";
        OleDbCommand dbCmd = new OleDbCommand(update, dbConn);
        dbCmd.Parameters.AddWithValue("@MemberID", memberid);
        dbCmd.Parameters.AddWithValue("@LastName", lastname);
        dbCmd.Parameters.AddWithValue("@FirstName", firstname);
        dbCmd.Parameters.AddWithValue("@Phone", phone);
        dbCmd.Parameters.AddWithValue("@E_mail", email);

        try
        {
            dbCmd.ExecuteNonQuery();
            MessageBox.Show("Update Complete");
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
            return;
        }

    }

So i run debug, i change one of the entries, i hit the modify member button, then i get a messagebox saying "The record cannot be deleted or changed because table 'Property' includes related records" Debug still goes and I don't get any errors.

Thank you.

You tried to perform an operation that would have violated referential integrity rules for related tables. For example, this error occurs if you try to delete or change a record in the "one" table in a one-to-many relationship when there are related records in the "many" table.

If you want to delete or change the record, first delete the related records from the "many" table. and in your case, you must be trying to update the foreign key column, which is referring to some other table's record.

From your code, one can easily assume that your column MemberID in table club_members , must be a foriegn key, referring to Member table's row. This is where you're making mess. you cannot violate the referential integrity by simply deleting/updating the record you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM