Click here to Skip to main content
14,739,804 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to get data from my database to a class library object, I'm using a stored procedure and dapper, my query works alright but what I need is to retrieve data from a particular table into my class library object, any ideas please?

What I have tried:

Public class sqlconnector IDataConnection:
Public TransactionsModel GetStockQuantity (TransactionsModel modell)
{
List<Transactions Model> output;
Using(IDbConnection connection= new MySqlConnection(GlobalConfig.CnnString(mydb)))
{
output = connection.Query<TransactionsModel>("Book_GetAll_By_BookTitle",new {book_titlesp=model.Transacted_book_name}, command type: CommandType.StoredProcedure).ToList();
var p= new DynamicParameter();
modell.QuantityAgain= p.Get<int>("quantity").ToString();
}
return model.MyQuantity;
}

The model.transacted book name gets into the query but it does give the output
When I run it it gives the exception system.collection.generic.key not found exception,the given key was not present in the dictionary at the command
Modell.QuantityAgain= p.Get<int>("quantity").ToString();
Posted
Updated 27-May-20 9:23am
Comments
Maciej Los 26-May-20 2:16am
   
What collation?

Quote:
var p= new DynamicParameter();
modell.QuantityAgain= p.Get<int>("quantity").ToString();
It's hardly surprising that Get fails, given that you've literally just created the DynamicParameters object, and haven't added any parameters to it!

Compare your code to the example in the documentation:
GitHub - StackExchange/Dapper: Dapper - a simple object mapper for .Net[^]
public int GetStockQuantity(TransactionsModel model)
{
    using (IDbConnection connection= new MySqlConnection(GlobalConfig.CnnString(mydb)))
    {
        var p = new DynamicParameter();
        p.Add("@book_titlesp", model.Transacted_book_name);
        p.Add("@quantity", dbType: DbType.Int32, direction: ParameterDirection.Output);
        
        connection.Execute("Book_GetAll_By_BookTitle", p, commandType: CommandType.StoredProcedure);
        
        return p.Get<int>("@quantity");
    }
}
   
v2
Comments
Maciej Los 27-May-20 16:07pm
   
You're magician, Richard!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900