Here is my code. I think the problem is with my declare @EmployeeId
.
string strCommand = "insert into Employee (FirstName, LastName, Birthday, Inn)" +
" values (@FirstName, @LastName, @Birthday, @Inn)" +
" declare @EmployeeId int = @@identity";
SqlCommand command = new SqlCommand(strCommand, connection);
command.Parameters.AddWithValue("@FirstName", employee.FirstName);
command.Parameters.AddWithValue("@LastName", employee.LastName);
command.Parameters.AddWithValue("@Birthday", employee.Birthday);
command.Parameters.AddWithValue("@Inn", employee.INN);
SqlParameter parameter = new SqlParameter("@EmployeeId", System.Data.SqlDbType.Int);
parameter.Direction = System.Data.ParameterDirection.Output;
//command.Parameters.Add(parameter); //???
try
{
connection.Open();
command.ExecuteNonQuery();
employee.EmployeeId = (int)parameter.Value; // Here I get the exception
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
// new Exception("Employee Not Insertes");
}
finally
{
connection.Close();
}
return employee;
this only one method that is not working at all. Sorry for my English.