InvalidCastException when querying MySql database with Dapper [closed]How do I quickly rename a MySQL database (change schema name)?How to output MySQL query results in CSV format?How do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?When to use struct?How to get a list of user accounts using the command line in MySQL?How to reset AUTO_INCREMENT in MySQL?How to get the sizes of the tables of a MySQL database?HttpClient.GetAsync(…) never returns when using await/asyncHow to import an SQL file using the command line in MySQL?

Alternative classical explanation of the Stern-Gerlach Experiment?

Referring to a character in 3rd person when they have amnesia

Why didn't Daenerys' advisers suggest assassinating Cersei?

Can I get the output of a command line program with TeX (using e.g. read18)?

Why does a table with a defined constant in its index compute 10X slower?

Can more than one instance of Bend Luck be applied to the same roll by multiple Wild Magic sorcerers?

What would be the game balance implications for using the Gygax method for applying falling damage?

Is it standard to have the first week's pay indefinitely withheld?

Is it a good idea to teach algorithm courses using pseudocode?

How do I balance a campaign consisting of four kobold PCs?

Managing heat dissipation in a magic wand

Quotient of Three Dimensional Torus by Permutation on Coordinates

Failing students when it might cause them economic ruin

pwaS eht tirsf dna tasl setterl fo hace dorw

on the truth quest vs in the quest for truth

Cycling to work - 30mile return

Gambler's Fallacy Dice

How to customize the pie chart background in PowerPoint?

Save my secrets!

Show that the characteristic polynomial is the same as the minimal polynomial

I recently started my machine learning PhD and I have absolutely no idea what I'm doing

How to pipe results multiple results into a command?

Parse a C++14 integer literal

What should I wear to go and sign an employment contract?



InvalidCastException when querying MySql database with Dapper [closed]


How do I quickly rename a MySQL database (change schema name)?How to output MySQL query results in CSV format?How do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?When to use struct?How to get a list of user accounts using the command line in MySQL?How to reset AUTO_INCREMENT in MySQL?How to get the sizes of the tables of a MySQL database?HttpClient.GetAsync(…) never returns when using await/asyncHow to import an SQL file using the command line in MySQL?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm using Dapper to query MySQL database from Linux server.



For my first query, really simple, I use this code:



public async Task<Customer> GetCustomerByCode(string customerCode)

using (var connection = GetOpenConnection())

Customer result = await connection.ExecuteScalarAsync<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);
return result;



protected IDbConnection GetOpenConnection()

var connection = new MySqlConnection(connectionString);
connection.Open();

return connection;


Trying to execute this query with an user code (existing) `PGM` I get this error:



System.InvalidCastException: Invalid cast from 'System.Int32' to 'EmemoriesSuite.Data.Entities.Customer'.



I'm not really understanding what I am doing wrong.. This should be my result query, and using it in PhpMyAdmin works like charme (select * from Customer where Code = 'PGM')










share|improve this question















closed as off-topic by Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi Mar 25 at 6:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi
If this question can be reworded to fit the rules in the help center, please edit the question.
















  • don't use ExecuteScalar

    – Daniel A. White
    Mar 23 at 17:58











  • @DanielA.White Hi, thanks for the hint! just to know, why shouldn't I use it? and what should I use instead?

    – Pier Giorgio Misley
    Mar 23 at 18:04






  • 3





    ExecuteScalar is used when you need a single object a string or an int from the database. What you need instead is connection.Query() this will return a resultset which can then be converted to object. What I also guess is you may need to use connection.Query().First()

    – Priyank Panchal
    Mar 23 at 18:05












  • oh, I didn't know that at all, thanks! @PriyankPanchal I'll give it a try

    – Pier Giorgio Misley
    Mar 23 at 18:06











  • @PriyankPanchal it works! thanks! if you post an answer I will accept it

    – Pier Giorgio Misley
    Mar 24 at 10:49

















0















I'm using Dapper to query MySQL database from Linux server.



For my first query, really simple, I use this code:



public async Task<Customer> GetCustomerByCode(string customerCode)

using (var connection = GetOpenConnection())

Customer result = await connection.ExecuteScalarAsync<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);
return result;



protected IDbConnection GetOpenConnection()

var connection = new MySqlConnection(connectionString);
connection.Open();

return connection;


Trying to execute this query with an user code (existing) `PGM` I get this error:



System.InvalidCastException: Invalid cast from 'System.Int32' to 'EmemoriesSuite.Data.Entities.Customer'.



I'm not really understanding what I am doing wrong.. This should be my result query, and using it in PhpMyAdmin works like charme (select * from Customer where Code = 'PGM')










share|improve this question















closed as off-topic by Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi Mar 25 at 6:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi
If this question can be reworded to fit the rules in the help center, please edit the question.
















  • don't use ExecuteScalar

    – Daniel A. White
    Mar 23 at 17:58











  • @DanielA.White Hi, thanks for the hint! just to know, why shouldn't I use it? and what should I use instead?

    – Pier Giorgio Misley
    Mar 23 at 18:04






  • 3





    ExecuteScalar is used when you need a single object a string or an int from the database. What you need instead is connection.Query() this will return a resultset which can then be converted to object. What I also guess is you may need to use connection.Query().First()

    – Priyank Panchal
    Mar 23 at 18:05












  • oh, I didn't know that at all, thanks! @PriyankPanchal I'll give it a try

    – Pier Giorgio Misley
    Mar 23 at 18:06











  • @PriyankPanchal it works! thanks! if you post an answer I will accept it

    – Pier Giorgio Misley
    Mar 24 at 10:49













0












0








0








I'm using Dapper to query MySQL database from Linux server.



For my first query, really simple, I use this code:



public async Task<Customer> GetCustomerByCode(string customerCode)

using (var connection = GetOpenConnection())

Customer result = await connection.ExecuteScalarAsync<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);
return result;



protected IDbConnection GetOpenConnection()

var connection = new MySqlConnection(connectionString);
connection.Open();

return connection;


Trying to execute this query with an user code (existing) `PGM` I get this error:



System.InvalidCastException: Invalid cast from 'System.Int32' to 'EmemoriesSuite.Data.Entities.Customer'.



I'm not really understanding what I am doing wrong.. This should be my result query, and using it in PhpMyAdmin works like charme (select * from Customer where Code = 'PGM')










share|improve this question
















I'm using Dapper to query MySQL database from Linux server.



For my first query, really simple, I use this code:



public async Task<Customer> GetCustomerByCode(string customerCode)

using (var connection = GetOpenConnection())

Customer result = await connection.ExecuteScalarAsync<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);
return result;



protected IDbConnection GetOpenConnection()

var connection = new MySqlConnection(connectionString);
connection.Open();

return connection;


Trying to execute this query with an user code (existing) `PGM` I get this error:



System.InvalidCastException: Invalid cast from 'System.Int32' to 'EmemoriesSuite.Data.Entities.Customer'.



I'm not really understanding what I am doing wrong.. This should be my result query, and using it in PhpMyAdmin works like charme (select * from Customer where Code = 'PGM')







c# mysql dapper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 17:57







Pier Giorgio Misley

















asked Mar 23 at 17:41









Pier Giorgio MisleyPier Giorgio Misley

3,71821643




3,71821643




closed as off-topic by Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi Mar 25 at 6:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi Mar 25 at 6:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Ian Kemp, Dmitry, thewaywewere, Sterling Archer, Amit Joshi
If this question can be reworded to fit the rules in the help center, please edit the question.












  • don't use ExecuteScalar

    – Daniel A. White
    Mar 23 at 17:58











  • @DanielA.White Hi, thanks for the hint! just to know, why shouldn't I use it? and what should I use instead?

    – Pier Giorgio Misley
    Mar 23 at 18:04






  • 3





    ExecuteScalar is used when you need a single object a string or an int from the database. What you need instead is connection.Query() this will return a resultset which can then be converted to object. What I also guess is you may need to use connection.Query().First()

    – Priyank Panchal
    Mar 23 at 18:05












  • oh, I didn't know that at all, thanks! @PriyankPanchal I'll give it a try

    – Pier Giorgio Misley
    Mar 23 at 18:06











  • @PriyankPanchal it works! thanks! if you post an answer I will accept it

    – Pier Giorgio Misley
    Mar 24 at 10:49

















  • don't use ExecuteScalar

    – Daniel A. White
    Mar 23 at 17:58











  • @DanielA.White Hi, thanks for the hint! just to know, why shouldn't I use it? and what should I use instead?

    – Pier Giorgio Misley
    Mar 23 at 18:04






  • 3





    ExecuteScalar is used when you need a single object a string or an int from the database. What you need instead is connection.Query() this will return a resultset which can then be converted to object. What I also guess is you may need to use connection.Query().First()

    – Priyank Panchal
    Mar 23 at 18:05












  • oh, I didn't know that at all, thanks! @PriyankPanchal I'll give it a try

    – Pier Giorgio Misley
    Mar 23 at 18:06











  • @PriyankPanchal it works! thanks! if you post an answer I will accept it

    – Pier Giorgio Misley
    Mar 24 at 10:49
















don't use ExecuteScalar

– Daniel A. White
Mar 23 at 17:58





don't use ExecuteScalar

– Daniel A. White
Mar 23 at 17:58













@DanielA.White Hi, thanks for the hint! just to know, why shouldn't I use it? and what should I use instead?

– Pier Giorgio Misley
Mar 23 at 18:04





@DanielA.White Hi, thanks for the hint! just to know, why shouldn't I use it? and what should I use instead?

– Pier Giorgio Misley
Mar 23 at 18:04




3




3





ExecuteScalar is used when you need a single object a string or an int from the database. What you need instead is connection.Query() this will return a resultset which can then be converted to object. What I also guess is you may need to use connection.Query().First()

– Priyank Panchal
Mar 23 at 18:05






ExecuteScalar is used when you need a single object a string or an int from the database. What you need instead is connection.Query() this will return a resultset which can then be converted to object. What I also guess is you may need to use connection.Query().First()

– Priyank Panchal
Mar 23 at 18:05














oh, I didn't know that at all, thanks! @PriyankPanchal I'll give it a try

– Pier Giorgio Misley
Mar 23 at 18:06





oh, I didn't know that at all, thanks! @PriyankPanchal I'll give it a try

– Pier Giorgio Misley
Mar 23 at 18:06













@PriyankPanchal it works! thanks! if you post an answer I will accept it

– Pier Giorgio Misley
Mar 24 at 10:49





@PriyankPanchal it works! thanks! if you post an answer I will accept it

– Pier Giorgio Misley
Mar 24 at 10:49












1 Answer
1






active

oldest

votes


















1














ExecuteScalar is used to get a single value from the database. Instead, what you need is connection.Query() which will return a resultset. Now, because you are expecting your query to return a single record you can use connection.QuerySingle(). So kindly use the code below.



Customer result = await connection.QuerySingle<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);


I have not tried it in practice, but I am very sure the above will work for you.






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    ExecuteScalar is used to get a single value from the database. Instead, what you need is connection.Query() which will return a resultset. Now, because you are expecting your query to return a single record you can use connection.QuerySingle(). So kindly use the code below.



    Customer result = await connection.QuerySingle<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);


    I have not tried it in practice, but I am very sure the above will work for you.






    share|improve this answer



























      1














      ExecuteScalar is used to get a single value from the database. Instead, what you need is connection.Query() which will return a resultset. Now, because you are expecting your query to return a single record you can use connection.QuerySingle(). So kindly use the code below.



      Customer result = await connection.QuerySingle<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);


      I have not tried it in practice, but I am very sure the above will work for you.






      share|improve this answer

























        1












        1








        1







        ExecuteScalar is used to get a single value from the database. Instead, what you need is connection.Query() which will return a resultset. Now, because you are expecting your query to return a single record you can use connection.QuerySingle(). So kindly use the code below.



        Customer result = await connection.QuerySingle<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);


        I have not tried it in practice, but I am very sure the above will work for you.






        share|improve this answer













        ExecuteScalar is used to get a single value from the database. Instead, what you need is connection.Query() which will return a resultset. Now, because you are expecting your query to return a single record you can use connection.QuerySingle(). So kindly use the code below.



        Customer result = await connection.QuerySingle<Customer>("select * from `Customer` where `Code` = @customerCode", new customerCode , null, null, System.Data.CommandType.Text);


        I have not tried it in practice, but I am very sure the above will work for you.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 11:36









        Priyank PanchalPriyank Panchal

        7241714




        7241714















            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript