Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.29/5 (4 votes)
See more:
how can i handle this error ("An explicit value for the identity column in table 'appointment' can only be specified when a column list is used and IDENTITY_INSERT is ON.")
Posted
Comments
CHill60 1-May-15 8:22am    
Share that caused the error. Share the code that caused the error
You have a column on your table defined as IDENTITY but you are trying to assign an explicit value to it in your code.
ZurdoDev 1-May-15 8:23am    
That IS the answer.
Herman<T>.Instance 1-May-15 8:28am    
it is as easy as that
Atta Ur Rahman 118 1-May-15 8:49am    
private void bt_requesting_Click(object sender, EventArgs e)
{
string requestappoint = combo_requstinpersonappoint.SelectedItem.ToString();
string requestpersonname = tb_requestingpersnname.Text.Trim();
string relation = tb_relation.Text.Trim();
string patientname = tb_patntnmae.Text.Trim();
string adres = tb_address.Text.Trim();
string city = tb_city.Text.Trim();
string cell = tb_cell.Text.Trim();
string patienttype = comboBox_patienttype.SelectedItem.ToString();
string apointtype = comboBox_appointtype.SelectedItem.ToString();
DateTime date = DateTime.Parse(desirdate.EditValue.ToString());
DateTime time = DateTime.Parse(desiretime.EditValue.ToString());
string insert = string.Format("insert into appointment values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')", requestappoint, requestpersonname, relation, patientname, adres, city, cell, patienttype, patienttype, desirdate, desiretime);
SqlCommand cmd = new SqlCommand(insert, dbcs.open());
cmd.ExecuteNonQuery();
MessageBox.Show("Request Complete");
}

Code is this
Atta Ur Rahman 118 1-May-15 8:50am    
private void bt_requesting_Click(object sender, EventArgs e)
{
string requestappoint = combo_requstinpersonappoint.SelectedItem.ToString();
string requestpersonname = tb_requestingpersnname.Text.Trim();
string relation = tb_relation.Text.Trim();
string patientname = tb_patntnmae.Text.Trim();
string adres = tb_address.Text.Trim();
string city = tb_city.Text.Trim();
string cell = tb_cell.Text.Trim();
string patienttype = comboBox_patienttype.SelectedItem.ToString();
string apointtype = comboBox_appointtype.SelectedItem.ToString();
DateTime date = DateTime.Parse(desirdate.EditValue.ToString());
DateTime time = DateTime.Parse(desiretime.EditValue.ToString());
string insert = string.Format("insert into appointment values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')", requestappoint, requestpersonname, relation, patientname, adres, city, cell, patienttype, patienttype, desirdate, desiretime);
SqlCommand cmd = new SqlCommand(insert, dbcs.open());
cmd.ExecuteNonQuery();
MessageBox.Show("Request Complete");
}
code is this.
I think the error is in this line (DateTime time = DateTime.Parse(desiretime.EditValue.ToString());)

1 solution

You have a column on your table [appointment] which has been defined as IDENTITY but in your code you are attempting to assign an explicit value to it.

Don't - the whole point of IDENTITY columns is that the database will generate those values for you.

An example:

SQL
create table Demo
(
	myID int IDENTITY(1,1),
	otherData varchar(30)
)
If I attempt to put data into that table using this
SQL
insert into Demo values
(1,'first datum'),
(2,'second datum'),
(3,'third datum')
then I will get an error reported
Quote:
Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'Demo' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Instead, I need to use
SQL
insert into Demo values
('first datum'),
('second datum'),
('third datum')
then when I select I get
myId    otherData
1	first datum
2	second datum
3	third datum
 
Share this answer
 
Comments
Manfred Rudolf Bihy 1-May-15 9:31am    
I keep on being astounded by how a very clear error message keeps people from finding the solution by themselves. Have a five! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900