Next code is giving me an exception:
cmd.CommandText = @"insert Table ";
cmd.CommandText += @"(StartTime,
EndTime)
values(@StartTime,
@EndTime)
SELECT CAST(scope_identity() AS int)";
cmd.Parameters.AddWithValue ( "@StartTime", DBNull.Value );
cmd.Parameters.AddWithValue ( "@EndTime", DBNull.Value );
cmd.ExecuteScalar();
The exception I am getting is Must declare '@StartTime' variable and same thing for @EndTime. Isn't the DBNull.Value used for things like this, what am I doing wrong?
INTOis optional.INSERT INTO TABLE2. There are not spaces betweentableand(starttime,EndTime)andvalues,@EndTime) andSELECT`INTOis optional. And also, I've got these spaces, this is just sample of the real code, so that's not the problem...@StarTimein your actual code? Try to debug further by usingSql Profilerto capture the actual prepared statement that is executed along with the sent parameters."insert Table (StartTime, EndTime) values(@StartTime, @EndTime) SELECT CAST(scope_identity() AS int)"?