Arianule
Arianule

Reputation: 9063

Error upon inserting data into Access database

I am busy on a school assignment and I have a basic access database that consists of three tables. Book, Reader and BookRated where as BookRated is the intersection table with Primary keys userName from Reader and ISBN from Book make up the composite primary key for BookRated(as foreign keys).

I am trying to insert information into the BookRated table but get the following error

System.Data.OleDb.OleDbException: You cannot add or change a record because a related record is required in table 'Book'.

bool added;

    int i;
    conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;
                                Data Source =" + Server.MapPath("App_Data\\BookRateInitial.mdb"));
    conn.Open();

    OleDbCommand cmd2 = conn.CreateCommand();
    cmd2.CommandText = @"INSERT INTO bookRated([frnISBN], [title], [rating], [review], [frnUserName])
                      VALUES(@ISBN, @title, @rating, @review, @userName)";
        //adding my parameters
    cmd2.Parameters.AddRange(new OleDbParameter[]
    {
        new OleDbParameter("@title", bookTitle),
        new OleDbParameter("@rating", rating),
        new OleDbParameter("@review", review), 
        new OleDbParameter("@ISBN", isbn), 
        new OleDbParameter("@userName", userName),
    });
    added = true;
    i = cmd2.ExecuteNonQuery();
    conn.Close();
    return added;

for now all the values are string.

Any advice perhaps as to where this error comes from?

Upvotes: 0

Views: 271

Answers (1)

Mayank Pathak
Mayank Pathak

Reputation: 3681

Check relationship of table bookRated. For more info on this error check Microsoft Support Site

Upvotes: 1

Related Questions