1

I am trying to call a stored procedure from entity framework but I am unable to get the output parameter value. It is always returning null. Here is my code

object[] parameters = {
    new SqlParameter("@Date", date),
    new SqlParameter("@outDate", SqlDbType.Date) {Direction= ParameterDirection.Output}
};
int defval = context.Database.CommandTimeout.Value;
context.Database.CommandTimeout = 1800;
context.Database.ExecuteSqlCommand("dbo.TestDateFormatAndRegion @Date, @outDate", parameters);
var NoOfSyncOperationsDeleted = parameters[1].ToString();

Can you please help.

Thanks

8
  • What happens if you use context.Database.ExecuteSqlCommand("dbo.TestDateFormatAndRegion @Date, @outDate OUTPUT", parameters);? Commented Sep 21, 2023 at 7:04
  • It actually executes the stored procedure but I am unable to get value in output parameter.
    – Razi Gohar
    Commented Sep 21, 2023 at 7:08
  • So including OUTPUT in the command made no difference? Commented Sep 21, 2023 at 7:09
  • No, it did not help.
    – Razi Gohar
    Commented Sep 21, 2023 at 7:13
  • Interesting... db<>fiddle Commented Sep 21, 2023 at 7:18

1 Answer 1

0
// Define the parameters array with input and output parameters
var parameters = new[]
{
    new SqlParameter("@Date", SqlDbType.Date) { Value = date },
    new SqlParameter("@outDate", SqlDbType.Date) { Direction = ParameterDirection.Output }
};

// Execute the stored procedure using ExecuteSqlCommand
context.Database.ExecuteSqlCommand("EXEC dbo.TestDateFormatAndRegion @Date, @outDate OUT", parameters);

// Retrieve the output parameter value after executing the stored procedure
var outDateValue = parameters[1].Value;


// Check if the output parameter value is not null before converting it to string
var NoOfSyncOperationsDeleted = outDateValue != DBNull.Value ? outDateValue.ToString() : "NULL";

// Restore the original command timeout if needed
context.Database.CommandTimeout = defval;

try this one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.