I was having issues converting a log file section to a SQL DateTime
and found a better solution was to have my date field generated by the database instead of code (using GETDATE()
).
I removed the date field from my insert but now I get this error:
Error: The parameterized query '(@station int, @indoor_temp float, @outdoor_temp float, @dewpoint f' expects the parameter '@wind_direction_text', which was not supplied.
That value it complains about is supplied, and what's stranger the insert still succeeds and the database record is inserted!
I'd like to figure out why I'm getting these exceptions though!
Here's my code:
var dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;
dataCommand.Parameters.AddWithValue("@station", _stationId);
dataCommand.Parameters.AddWithValue("@indoor_temp", _indoorTemp);
dataCommand.Parameters.AddWithValue("@outdoor_temp", _outdoorTemp);
dataCommand.Parameters.AddWithValue("@dewpoint", _dewPoint);
dataCommand.Parameters.AddWithValue("@rel_humidity_indoor", _relHumidityIndoor);
dataCommand.Parameters.AddWithValue("@rel_humidity_outdoor", _relHumidityOutdoor);
dataCommand.Parameters.AddWithValue("@wind_speed", _windSpeed);
dataCommand.Parameters.AddWithValue("@wind_direction_degrees", _windDirectionDegrees);
dataCommand.Parameters.AddWithValue("@wind_direction_text", _windDirectionText);
dataCommand.Parameters.AddWithValue("@wind_chill", _windChill);
dataCommand.Parameters.AddWithValue("@rain1h", _rain1H);
dataCommand.Parameters.AddWithValue("@rain24h", _rain24H);
dataCommand.Parameters.AddWithValue("@rain_total", _totalRain);
dataCommand.Parameters.AddWithValue("@relative_pressure", _relativePressure);
dataCommand.Parameters.AddWithValue("@tendency", _tendency);
dataCommand.Parameters.AddWithValue("@forecast", _forecast);
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText =
"INSERT INTO"
+ " dbo.WeatherData(station_id, indoor_temp, outdoor_temp, dewpoint," +
"rel_humidity_indoor, rel_humidity_outdoor, wind_speed, wind_direction_degrees," +
"wind_direction_text, wind_chill, rain1h, rain24h, rain_total, relative_pressure," +
"tendency, forecast)"
+ "VALUES " +
"(@station, @indoor_temp, @outdoor_temp, @dewpoint, @rel_humidity_indoor," +
"@rel_humidity_outdoor, @wind_speed, @wind_direction_degrees," +
"@wind_direction_text, @wind_chill, @rain1h, @rain24h, @rain_total," +
"@relative_pressure, @tendency, @forecast)";
// execute the insert command
dataCommand.ExecuteNonQuery();
Thanks for your help!