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;
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
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
|
show 1 more comment
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
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
don't useExecuteScalar
– 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 isconnection.Query()
this will return a resultset which can then be converted to object. What I also guess is you may need to useconnection.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
|
show 1 more comment
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
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
c# mysql dapper
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
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
don't useExecuteScalar
– 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 isconnection.Query()
this will return a resultset which can then be converted to object. What I also guess is you may need to useconnection.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
|
show 1 more comment
don't useExecuteScalar
– 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 isconnection.Query()
this will return a resultset which can then be converted to object. What I also guess is you may need to useconnection.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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 24 at 11:36
Priyank PanchalPriyank Panchal
7241714
7241714
add a comment |
add a comment |
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 useconnection.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