I am attempting an INSERT statement with a nested SELECT statement. Both of the database fields are of int type.
My statement:
energy_command = New SqlCommand("INSERT INTO energy ([ApplicationID], [usage], [policy], [audit], [lighting], [over_lighting], [controls], [turn_off], [energy_star], [weatherize],[thermostat], [natural_light], [air], [renewable_assessment], [on_site_renewable], [HVAC],[renewable_power], [efficient_enroll], [efficient_attain], [other]) " &
"VALUES ('(SELECT ApplicationID FROM general where Business_Name = """ & txtName.Text & """)', '" & track_usage & "', '" & develop_strategy & "', '" & perform_audit & "', '" & replace_bulbs & "', '" & reduce_lighting & "', '" & lighting_controls & "', '" & not_in_use_policy & "', '" & energy_star & "', '" & weatherize & "', '" & program_thermo & "', '" & natural_light & "', '" & air_quality & "', '" & site_assessment & "', '" & renewable_power & "', '" & HVAC & "', '" & renewable_energy & "', '" & energy_programs & "', '" & energy_certification & "', '" & energy_other & "')", connection)`
My error:
System.Data.SqlClient.SqlException: Conversion failed when converting
the varchar value ‘(SELECT ApplicationID FROM general where
Business_Name = “a”)’ to data type int.
My only thought is that it’s trying to insert the entire SELECT statement into the INT field. Am I wrong? How can I fix this?
Because you put a single quote to quote the query, so it is considered as a string. You should remove the single quote of your query just this