I created this table in SQL Server to store data about a student's fees:
create table Fee(
feeID int primary key identity(1,1),
classID int foreign key references Class(classID),
nameID int foreign key references Students(studentsID),
firstNameID int foreign key references Students(studentsID),
dateOfAdmission varchar(50),
monthKey int foreign key references Month(monthID), sum int
)
And in Visual Studio I tried to save the data in the table with the following method:
private void btn_fee_Save_Click(object sender, EventArgs e)
{
int Class = Convert.ToInt32(cmbFeeClass.SelectedValue.ToString());
int Name = Convert.ToInt32(cmbFeeName.SelectedValue.ToString());
int firstName = Convert.ToInt32(cmbFeeFirstName.SelectedValue.ToString());
int Month = Convert.ToInt32(cmbMonth.SelectedValue.ToString());
try {
if (cmbFeeClass.Text != "" && cmbFeeName.Text != "" && cmbFeeFirstName.Text != "" && cmbMonth.Text != "" && txtSum.Text != "")
{
cmd = new SqlCommand("insert into Fee values('"+ Class + "', '" + Name + "', '" + firstName + "', " +
"'" + dtDateOfAdmission.Text + "', '" + Month + "', '" + "', '" + txtSum.Text + "')", con.ConnectionOpening());
cmd.ExecuteNonQuery();
MessageBox.Show("The data has been saved", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Please fill in all the fields!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.CloseConnection();
}
How can I change the code so that I no longer have this error:
An explicit value for the identity column in table 'Fee' can only be specified when a column list is used and IDENTITY_INSERT is ON