I’m getting
“An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON.”
Whether I specify the identity or not, the exact same error. I do not want to specify the identity and I’m not inserting it anywhere I can see atleast. Here is my (edit, jcolebrand) “relevant” C# code:
SqlConnection con = new SqlConnection(strcon);
this.getControls(this.Controls);
con.Open();
/* jcolebrand moved the sql query text further down, it's long */
SqlCommand cmd = new SqlCommand(query, con);
cmd = getVals(cmd);
cmd.Parameters.AddWithValue("event_link", DBNull.Value);
cmd.Parameters.AddWithValue("event_approved", DBNull.Value);
cmd.Parameters.AddWithValue("event_in_csi", DBNull.Value);
cmd.Parameters.AddWithValue("event_in_website", DBNull.Value);
cmd.Parameters.AddWithValue("date_added", DateTime.Now.ToString());
cmd.Parameters.AddWithValue("event_start_time1", DBNull.Value);
cmd.Parameters.AddWithValue("event_end_time1", DBNull.Value);
cmd.Parameters.AddWithValue("program", DBNull.Value);
cmd.Parameters.AddWithValue("event_owner", DBNull.Value);
cmd.ExecuteNonQuery();
con.Close();
String query = @"INSERT INTO [rec_serv].[dbo].[recserv_event1] (
event_name
, event_category
, event_location
, event_instructor
, event_start_date
, event_end_date
, event_start_time
, event_end_time
, event_deadline
, member_fee
, non_member_fee
, email
, registration
, minimum_size
, maximum_size
, waiting_list
, event_detail
, monday
, tuesday
, wednesday
, thursday
, friday
, saturday
, sunday
, number_handout
, color_handout
, size_handout
, number_poster
, color_poster
, size_poster
, listserv
, news_release
, web_banner
, visix_ad
, desired_displayed_date
, other1
, other2
, comment
, event_link
, event_approved
, event_in_csi
, event_in_website
, date_added
, extracomment
, receiptinfo
, program
, event_owner
, event_start_time1
, event_end_time1
) VALUES (
@event_name,
@event_category,
@event_location,
@event_instructor,
@event_start_date,
@event_end_date,
@event_start_time,
@event_end_time,
@event_deadline,
@member_fee,
@non_member_fee,
@email,
@registration,
@minimum_size,
@maximum_size,
@waiting_list,
@event_detail,
@monday,
@tuesday,
@wednesday,
@thursday,
@friday,
@saturday,
@sunday,
@number_handout,
@color_handout,
@size_handout,
@number_poster,
@color_poster,
@size_poster,
@listserv,
@news_release,
@web_banner,
@visix_ad,
@desired_displayed_date,
@other1,
@other2,
@comment,
@event_link,
@event_approved,
@event_in_csi,
@event_in_website,
@date_added,
@extracomment,
@receiptinfo,
@program,
@event_owner,
@event_start_time1,
@event_end_time1
);"
You should use explicit columns when possible in SQL. So, for this query, it would change the form of the query to:
This is also important because it more clearly defines the contract between the application and the SQL database, allowing for future changes.