M4NG20
M4NG20

Reputation: 25

Error "Column name or number of supplied values does not match table definition"

I'm currently learning to work with databases and SQL. I'm getting an error which I can't seem to resolve. The error I'm getting is stated in the title.

This is my table definition:

CREATE TABLE [dbo].[Filmstbl] 
(
    [Id]             INT            NOT NULL IDENTITY,
    [FilmTitel]      NVARCHAR (50)  NULL,
    [Duratie]        NVARCHAR (50)  NULL,
    [Genre]          NVARCHAR (50)  NULL,
    [Director]       NVARCHAR (50)  NULL,
    [Acteur]         NVARCHAR (50)  NULL,
    [JaarVanUitgave] NVARCHAR (50)  NULL,
    [Omschrijving]   NVARCHAR (MAX) NULL,
    [GastID]         INT            NULL,

    PRIMARY KEY CLUSTERED ([Id] ASC)
);

The query im using looks like this:

public void AddFilms(string titel, string lengte, string genre, string director, string acteur, string jaaruitgave, string omschrijving, int PersoonID)
{
    using (SqlConnection connection = new SqlConnection(ConnectionString))
    {
        connection.Open();

        string query = "INSERT INTO Filmstbl Values (@FilmTitel, @Duratie, @Genre, @Director, @Acteur, @JaarVanUitgave, @Omschrijving, @GastID)";

        using (SqlCommand addfilms = new SqlCommand(query, connection))
        {
            addfilms.Parameters.AddWithValue("@FilmTitel", titel);
            addfilms.Parameters.AddWithValue("@Duratie", lengte);
            addfilms.Parameters.AddWithValue("@Genre", genre);
            addfilms.Parameters.AddWithValue("@Director", director);
            addfilms.Parameters.AddWithValue("@Acteur", acteur);
            addfilms.Parameters.AddWithValue("@JaarVanUitgave", jaaruitgave);
            addfilms.Parameters.AddWithValue("@Omschrijving", omschrijving);
            addfilms.Parameters.AddWithValue("@GastID", PersoonID);
            addfilms.ExecuteNonQuery();
        }
    }
}

The datatypes are pretty much always a random string, except the last parameter which is a random int.

private void FilmToevoegenBtn_Click(object sender, EventArgs e)
{
    sql.AddFilms(FilmTitelTBox.Text, FilmLengteTBox.Text, FilmGenreTBox.Text, FilmDirectorTBox.Text, FilmActeurTBox.Text, FilmJaarUitgaveTBox.Text, FilmOmschrijvingTBox.Text, gebruiker.GebruikerID);
    UpdateListBoxes();
}

I'm adding data to another table the same way without a problem, so i'm wondering why it doesn't work with this one. I hope someone knows the answer. Thank you.

Upvotes: 0

Views: 881

Answers (1)

Janaka Neranjan
Janaka Neranjan

Reputation: 26

Please re-check table ID field.It should be auto increment with 1. e.g.:

[Id] [int] IDENTITY(1,1)

Upvotes: 1

Related Questions