locked
Error upon inserting data into Access database(asp.net, C#) RRS feed

  • Question

  • 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;

    Any advice perhaps

    Kind regards

    • Moved by Chester Hong Wednesday, November 14, 2012 7:26 AM problem in ASP.NET (From:Windows Forms Data Controls and Databinding)
    Thursday, November 8, 2012 9:59 AM

Answers