user1524116
user1524116

Reputation: 75

Inserting into a table, how to insert a auto incrementing ID

I am trying to insert data into a SQL table but getting an error that I do not have the required number of columns, e.g. I only give it teamname and teamtag, when it also had ID in the SQL table. The ID auto increments though so I am not sure how to make this work, any help is appreciated.

private void btnAdd_Click(object sender, EventArgs e)
{
    da.InsertCommand = new SqlCommand("INSERT INTO tblTeams VALUES(@TEAMNAME, @TEAMTAG)", cs);
    da.InsertCommand.Parameters.Add("@TEAMNAME", SqlDbType.VarChar).Value = txtTeamName.Text;
    da.InsertCommand.Parameters.Add("@TEAMTAG", SqlDbType.VarChar).Value = txtTeamTag.Text;

    cs.Open();
    da.InsertCommand.ExecuteNonQuery();
    cs.Close();
}

Error:

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

Upvotes: 0

Views: 1651

Answers (1)

Paul Fleming
Paul Fleming

Reputation: 24526

Specify the columns you are populating.

INSERT INTO tblTeams (TeamName, TeamTag) VALUES(@TEAMNAME, @TEAMTAG)

Upvotes: 4

Related Questions