1

can I directly pass the parameter in the query as shown below?

There is error so anyone could help?

public IEnumerable ListTop10countries(string direction)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCON"].ToString()))
    {
        var list = con.Query<OutputCountries>("Usp_GetTop10", direction).AsEnumerable();       
        return list;
    }
}

Error:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Procedure or function 'Usp_GetTop10' expects parameter '@direction', which was not supplied.

The stored proc script as is below:

CREATE proc [dbo].[Usp_GetTop10]
@direction nvarchar(30)
as
begin
SELECT TOP 10 
      [country]    
      ,[Tons]
  FROM [master].[dbo].[a]
  where direction = @direction
  order by [Tons] Desc
end
GO
Dirk
  • 10,668
  • 2
  • 35
  • 49
Nel
  • 63
  • 2
  • 9

1 Answers1

1

try this

public IEnumerable ListTop10countries(string mydirection)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCON"].ToString()))
    {
         var list = con.Query<OutputCountries>("Usp_GetTop10", new {direction = mydirection}, commandType: CommandType.StoredProcedure).AsEnumerable();
         return list;
    }
}
sangram parmar
  • 8,462
  • 2
  • 23
  • 47
  • hi @sangramparmar can you expound on this answer and connect the dots a bit more for me. I"m sorry for asking this but this is a bit advance for me, i'm trying to figure out if Query is an extension method and for the type OutputCountries how do i create that type. thanks in advance. – skinnyWill Oct 15 '19 at 21:46
  • hi @Nel are you able to add on this. I was trying to implement this but its a bit advance for me. I think your implementing an extension method, with a type of Output countries. Do you have some more code you can add. Thanks – skinnyWill Oct 16 '19 at 13:24