1

My school project is to make a website where people can sell/buy used school items like calculators etc. I am using ASP.net & vb.net in Microsoft Visual Studio.

My problem is that I don't really get how to add the data filled in on the website, inserted into the MS Access database file. The problem is that the information is spread over 2 tables.

I've tried to use 2 different SQL statements, 1 for each table, but this doesn't work out either. I've changed the format of the database a bit and I think that now the problem is to input the relation between the 2 tables but I have no idea how to.

I have this code running right now

    Protected Sub bntVerzenden_Click(sender As Object, e As EventArgs) Handles bntVerzenden.Click



    Dim VerkopenConn As New OleDb.OleDbConnection
    Dim VerkopenCommArtikel, VerkopenCommGebruiker As New OleDb.OleDbCommand

    VerkopenConn.ConnectionString = My.Settings.SvShopConnection
    VerkopenCommArtikel.Connection = VerkopenConn
    VerkopenCommGebruiker.Connection = VerkopenConn

    'Gebruiker gegevens aan de database toevoegen
    VerkopenCommGebruiker.CommandText = "INSERT INTO tblGebruiker (GebruikersNaam, GebruikersVoornaam, GebruikersEmail, GebruikersGSM) VALUES (@GebruikersNaam, @GebruikersVoornaam, @GebruikersEmail, @GebruikersGSM)"

    VerkopenCommGebruiker.Parameters.AddWithValue("@GebruikersNaam", txtNaam.Text)
    VerkopenCommGebruiker.Parameters.AddWithValue("@GebruikersVoornaam", txtVNaam.Text)
    VerkopenCommGebruiker.Parameters.AddWithValue("@GebruikersEmail", txtEmail.Text)
    VerkopenCommGebruiker.Parameters.AddWithValue("@GebruikersGSM", txtGsm.Text)

    'Artikel gegevens aan de database toevoegen
    VerkopenCommArtikel.CommandText = "INSERT INTO tblArtikel (ArtikelBeschrijving, ArtikelPrijs, RubriekNaam, ArtikelAankoopdatum, ArtikelTekoopgezet, ArtikelBeschikbaar) VALUES ( @ArtikelBeschrijving, @ArtikelPrijs, @RubriekNaam, @ArtikelAankoopdatum, @ArtikelTekoopgezet, @ArtikelBeschikbaar)"

    VerkopenCommArtikel.Parameters.AddWithValue("@ArtikelBeschrijving", txtArtikelBeschrijving.Text)
    VerkopenCommArtikel.Parameters.AddWithValue("@ArtikelPrijs", txtArtikelPrijs.Text)
    VerkopenCommArtikel.Parameters.AddWithValue("@RubriekNaam", lstRubriek.SelectedItem.ToString)
    VerkopenCommArtikel.Parameters.AddWithValue("@ArtikelAankoopdatum", CalAankoop.SelectedDate)
    VerkopenCommArtikel.Parameters.AddWithValue("@ArtikelTekoopgezet", calVerkoop.SelectedDate)
    VerkopenCommArtikel.Parameters.AddWithValue("@ArtikelBeschikbaar", True)

    If VerkopenConn.State = ConnectionState.Closed Then VerkopenConn.Open()
    VerkopenCommGebruiker.ExecuteNonQuery()
    VerkopenCommArtikel.ExecuteNonQuery()






End Sub

End Class

With this error at the second ExecuteNonQuery:

An exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll but was not handled in user code

Additional information: You cannot add or change a record because a related record is required in table 'tblGebruiker'.

The first query works and that data has been added to the database.

Database relations down below.

Relations

10
  • Does it give any errors when it runs? Also, could you post some details about your access database? Commented May 5, 2016 at 14:06
  • 1
    You're missing RubriekID in your columns section of your second query. Commented May 5, 2016 at 14:11
  • @amber Edited the code and edited the question. Commented May 5, 2016 at 15:24
  • @lamelemon the relations can be seen in this post. Commented May 5, 2016 at 15:25
  • @ThimoDemey Yeah, I saw that afterwards again. Just as a test, try removing the relationship between tblArtikel and tblGebruiker. Then run the query. Commented May 5, 2016 at 15:35

1 Answer 1

1

Your first exception was because you were missing a column in your query. This second exception is PROBABLY caused by you having a one-to-one relationship defined between tblArtikel and tblRubriek. You need to change the relationship to one-to-many to allow duplcates, in this case. tblRubriek would be the "one" side and tblArtikel would be the "many" side. Because you have one primary key in tblRubriek that associates with "many" rows in tblArtikel.

Sign up to request clarification or add additional context in comments.

4 Comments

So in 'tblArtikel' do I have to use 'RubriekNaam' instead of 'RubriekID' and make the relation with 'RubriekNaam' in 'tblRubriek' , right?
RubriekID is not a primary key in tblArtikel, so it shouldn't be a problem to insert it, but you do need to mention that it need to be inserted "INSERT INTO tblArtikel (RubriekID, ArtikelBeschrijving, ArtikelPrijs, ArtikelAankoopdatum, ArtikelTekoopgezet) VALUES (@RubriekID, @ArtikelBeschrijving, @ArtikelPrijs, @ArtikelAankoopdatum, @ArtikelTekoopgezet)"
@ThimoDemey Sorry. As I mentioned above, the query that Amber posted is what you should have. But the relationship you defined may be the issue.
I've changed the query but still have the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.