I want to run query in EF but when I run this query in SQL SERVER MANAGEMENT THEN return "00002" but when I run in EF, this query return -1. I don't know what is happing any senior can you tell where i am wrong. I also share the screenshot of the query result where you can see the return result. error: "-1".
I am sharing the C# function where I fetch the query from the database and call the function using jquery. function working properly but query result is wrong . Why? I don't know.
when i use SQLQUERY then it will return whole query as a string like "
select RIGHT('00000' + cast(ISNULL(MAX(User_Id),0)+1 as varchar(5)) + '',6)from secUsers
"
C#
- public int Maxnumber()
- {
- int result = _db.Database.ExecuteSqlCommand("select RIGHT('00000' + cast(ISNULL(MAX(User_Id),0)+1 as varchar(5)) + '',6)from secUsers");
- return result;
- }
- public JsonResult max_UserID()
- {
- var mNo= _IAccount.Maxnumber();
- return Json(mNo, JsonRequestBehavior.AllowGet);
- }
- function MaxNo() {
- $.ajax({
- type: 'GET',
- url: '@Url.Action("max_UserID")',
- dataType: 'json',
- data: {},
- success: function (mNo) {
- $("#userid").val(mNo);
- },
- error: function (ex) {
- var r = jQuery.parseJSON(response.responseText);
- alert("Message: " + r.Message);
- alert("StackTrace: " + r.StackTrace);
- alert("ExceptionType: " + r.ExceptionType);
- }
- });
- return false;
- }
Laxmidhar SahooPosted Jul 9, 2020, 3:19 AM
Jenith ThakkarPosted Jul 8, 2020, 11:31 PM
Write your query in this way
int result = _db.Database.SqlQuery
You had use ExecuteSqlCommand Which is return only status of query. so you can use that for insert and update statement but here you want to do some select operation so use SqlQuery
If it works then except my answer
Thank you