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.
RubriekIDin your columns section of your second query.tblArtikelandtblGebruiker. Then run the query.