error from linq while execting stored procedure from linq - c#

I need to execute SQL Server stored procedure from linq-to-sql. I added my stored procedure in data context and used the context in my class. Here is my code to call sql server stored procedure from c# class:
using (DBTestDataContext db = new DBTestDataContext())
{
db.myStoreProcedure(name, address, hasValue);
}
Here, hasValue is of bit type, name and address are of string type and i have pass value in hasValue as true/false making it a bool type. My issue is as the procedure is executed, I am getting the error
Could not translate expression 'value (DBTestDataContext.myStoreProcedure("abc","xyz",true)) into SQL and could not treat it as a local expression.
However, it executed successfully in sql server:
EXEC dbo.myStoreProcedure #name="abc", #address = "xyz", #hasValue = true
it is working fine. Is this the issue associated with db.myStoreProcedure ??? For any data entered, this code is not working. How can it be fixed
Can anyone help me how to fix this. thanks in advance..

Try executing it like this .
var param1 = new SqlParameter("#param1", 1);
var param2 = new SqlParameter("#param2", 2);
var results = context.Database.SqlQuery<StronglyTypedObject>
("SPName #param1, #param2",param1, param2).ToList()

Related

DynamicParam AddDynamicParameters not passing data to stored procedure with Dapper (parameter not supplied error)

I've got the following stored procedure in SQL Server
CREATE PROCEDURE [testingDynamic]
#stringa VARCHAR(50),
#datea DATETIME
AS
BEGIN
SELECT #stringa + #datea
END
And this is where it's used in C#
public async Task<string> testingDynamic(string? stringa, Nullable<DateTime> datea)
{
var param = new DynamicParameters(stringa);
param.AddDynamicParams(datea);
string res = await db.ExecuteScalarAsync<string>("testingDynamic", param, commandType: CommandType.StoredProcedure).ConfigureAwait(false);
return res;
}
When calling the method testingDynamic, both parameters have the value of null (I need to check something this way)
When I run it, I get an error
Procedure or function 'testingDynamic' expects parameter '#stringa', which was not supplied
Funny thing, if I populate param using
param.Add("#stringa", stringa);
or even:
.AddDynamicParameters(new { stringa = stringa, datea = datea });
it works just fine.
But if I could use new { ... } I wouldn't need DynamicParameters
Any idea what's causing the problem?
EDIT: now I know that ADO.NET sends string to database as NVARCHAR when using AddWithValue() and my stored procedure parameter is VARCHAR, but I'm using Dapper with AddDynamicParameters().
Does Dapper have the same issue?

Pass integer array to the SQL Server stored procedure

Well this well answered question however I can't get it right for me. What I am trying to do call a stored procedure from .NET Core project using Entity Framework with some parameters. One of those parameter should be array (which I consider table type in SQL Server by create a custom table data type) type. I followed this Stackoverflow link. But got an error when I tried to execute my SQL command.
Here is my code:
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
foreach (var section in model.VMSectionIds) //model.VMSectionIds contains set of integers
{
dt.Rows.Add(section);
}
and finally I call stored procedure like this:
var sectiolist = new SqlParameter("#Sections", SqlDbType.Structured)
{
TypeName = "[dbo].[SectionList]",
Value = dt
};
_db.ExecuteSqlCommand("EXEC [SP_GenerateRegularEmployeeSalary] "+mastermodel.ID+","+ fromdate + "," + todate + ",1," + sectiolist + ""); //don't worry I took care of SQL injection for others parameter
But this execution throws an exception
SqlException: Must declare the scalar variable "#Sections"
I can't figure it out where exact problem is. Here call of stored procedure (with some static test parameter) from SQL for clear understanding of my stored procedure call mechanism:
DECLARE #data [SectionList]
INSERT #data (Id) VALUES (2, 3)
EXEC [SP_GenerateRegularEmployeeSalary] 2,'20190401','20190430','1',#data
Looks that you are using the ExecuteSqlCommand incorrectly. Try this way and don't use string concatenation in your code to avoid SQL Injection attacks in your application. Read more about it here.
Also put the correct expected parameter names from the stored procedure: SP_GenerateRegularEmployeeSalary.
Option 1
_db.ExecuteSqlCommand("EXEC [SP_GenerateRegularEmployeeSalary] #ID, #FromDate, #ToDate, #Flag, #Sections",
new SqlParameter("#ID", mastermodel.ID),
new SqlParameter("#FromDate", fromdate),
new SqlParameter("#ToDate", todate),
new SqlParameter("#Flag", 1),
new SqlParameter("#Sections", sectiolist));
Option 2
_db.ExecuteSqlCommand("EXEC [SP_GenerateRegularEmployeeSalary] #ID = {0}, #FromDate = {1}, #ToDate = {2}, #Flag = 1, #Sections = {4}", mastermodel.ID, fromdate, todate, sectiolist);
Please read this documentation about this method.
he using ExecuteSqlCommand incorrectly. he should not used string concatenation to avoid SQL Injection attacks in th application
_db.ExecuteSqlCommand("EXEC SP_GenerateRegularEmployeeSalary #YOUR_PARAM_ON_STOREPROCEDURE", sectiolist);

Why is data not stored in database from stored procedure?

I'm using Entity Framework and I'm trying to store some data in SQL Server using this stored procedure:
create procedure [dbo].[Sp_OrderList]
#OrderId int,
#OrderName varchar(50),
#OrderData datetime
As
Begin
Insert into OrderList (OrderId, OrderName, OrderDate)
values (#OrderId, #OrderName, #OrderData)
End
C# code:
public HttpResponseMessage SaveCountry(OrderList cnt)
{
OrderList Cntrcountr = new OrderList();
Cntrcountr.OrderId = cnt.OrderId;
Cntrcountr.OrderName = cnt.OrderName;
Cntrcountr.OrderDate = cnt.OrderDate;
var param = new SqlParameter("",Cntrcountr);
var country = db.Database.SqlQuery<string>("Sp_OrderList", Cntrcountr);
return Request.CreateResponse(HttpStatusCode.OK, country);
}
I'm getting an error
500 Internal server error
Please help me - what's my mistake?
Since your stored procedure doesn't actually return any data you should use the ExecuteSqlCommand method. Try this:
db.Database.ExecuteSqlCommand(
"EXEC Sp_OrderList #OrderId, #OrderName, #OrderData",
new SqlParameter("#OrderId", cnt.OrderId),
new SqlParameter("#OrderName", cnt.OrderName),
new SqlParameter("#OrderData", cnt.OrderData));
Database.SqlQuery takes the sql and then parameters as an array. You are passing in a complex objects and expecting the properties to be translated into parameters. See Database.SqlQuery documentation.
public HttpResponseMessage SaveCountry(OrderList cnt)
{
var country = db.Database.SqlQuery<string>("exec Sp_OrderList #OrderId, #OrderName, #OrderData", cnt.OrderId, cnt.OrderName, cnt.OrderDate);
return Request.CreateResponse(HttpStatusCode.OK, country);
}
That said the reason for the 500 status is because your server code has experienced an unhandled Exception. You should add logging OR learn how to debug your server side code so you can inspect the Exception when it occurs.

Can't get stored procedure results with Entity Framework 6

I have a stored procedure which returns a 0 or a 1 depending on whether or not a specified email address exists in my database:
CREATE PROCEDURE [DatabaseSchema].[EmailAddressIsDuplicate] (#emailAddress nvarchar(255))
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(
SELECT *
FROM [DatabaseSchema].[EmailUpdatesRegistrant]
WHERE EmailAddress = #emailAddress
)
RETURN 1
ELSE
RETURN 0
RETURN 0
END
GO
And I'm trying to derive the results of this stored procedure from an Entity Framework 6 database context:
using (DatabaseContext dbContext = new DatabaseContext())
{
ObjectParameter param = new ObjectParameter("emailAddress", typeof(bool));
var result = dbContext.EmailAddressIsDuplicate(emailAddress);
}
I'm getting lots of errors.
Error #1: Using the code above, var result is always set to -1.
Error #2: I tried navigated to Edit Function Import and set the Returns a Collection Of to a Boolean scalar value. This throws the following error:
The data reader returned by the store data provider does not have enough columns for the query requested.
Error #3: I went back and set the Edit Function Import return value to None. Then I tried the following code from this answer:
using (DatabaseContext dbContext = new DatabaseContext())
{
var p = new SqlParameter("#emailAddress", emailAddress);
var result = dbContext.Database.SqlQuery<bool>("DatabaseSchema.EmailAddressIsDuplicate", p);
}
No immediate errors thrown, but I have no idea whether or not I can derive useful data from var result. Trying to cast result to bool throws the following error:
Cannot convert type 'System.Data.Entity.Infrastructure.DbRawSqlQuery' to 'bool'
Any ideas on how I can see the results of this stored procedure (0 or 1)?
You could try adding an output parameter (#result) in the stored procedure signature:
CREATE PROCEDURE [DatabaseSchema].[EmailAddressIsDuplicate]
(#emailAddress nvarchar(255), #result bit out)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT *
FROM [DatabaseSchema].[EmailUpdatesRegistrant]
WHERE EmailAddress = #emailAddress)
SET #result = 1
ELSE
SET #result = 0
RETURN #result
END
GO
(you'll have to re-define your EF Model Function definition accordingly)
using (DatabaseContext dbContext = new DatabaseContext())
{
ObjectParameter isDuplicate = new ObjectParameter("isDuplicate", typeof(bool));
var result = dbContext.EmailAddressIsDuplicate(emailAddress, isDuplicate);
bool emailIsDuplicate = (bool)isDuplicate.Value;.
}
If you want to call the stored procedure directly with an out parameter you could follow this suggestion:
Database.SqlQuery calling stored procedure that has multiple output parameters
REASON - The template builder for EF (including v6) incorrectly sets the SP up as returning an INT containing the row count rather than the return value because it incorrectly calls the wrong ObjectContext.ExecuteFunction (found in the template-generated class YourDatabaseEntities that is the child of the DBContext).
Why wrong ExecuteFunction? - The result set incorrectly says the row count of changed rows rather than the return value or output parameters because it calls a different ExecuteFunction that discards the results. The flyover intellisense hint of the ObjectContext.ExecuteFunction says "Executes a stored procedure ….; discards any results returned from the function; and returns the number of rows affected by the execution" rather than the usual "Executes a stored procedure …. with the specified parameters".
WHY -1: I believe the SET NOCOUNT ON is causing the SP to return no count result and that Microsoft's ExecuteFunction returns that as error code.
SP FIXES - 1) You have to comment out SET NOCOUNT ON .
2) You have to change stored procedure to do the SELECT command as last statement instead of the RETURN command.
SOLUTION FIX - 1) After fixing SP, delete SP from Function Imports folder and the Data Store's SP folder. 2) Reload the SP into the EDMX by using the "Update Model from Database" 3) Rebuild all of your data project where the EDMX resides. 4) Exit Visual Studio and return. 5) Rebuild overall solution.
See: Entity Framework (Database first) has incorrect return result from stored procedure
Implement the stored procedure in C# to a value using parameters.
Resource: https://msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.110).aspx
This way, the values can be stored to a variable from the ExecuteReader.
Add the value to model similar to adding a value to a property. The stored procedure could be called from ActionResult. Though this may require adding the stored procedure to a separate layer, that simply runs the stored procedure and adds the value to model afterwards.
try this
CREATE PROCEDURE [DatabaseSchema].[EmailAddressIsDuplicate] (#emailAddress nvarchar(255))
AS
BEGIN
SELECT *
FROM [DatabaseSchema].[EmailUpdatesRegistrant]
WHERE EmailAddress = #emailAddress
SELECT ##ROWCOUNT
END
GO
using (DatabaseContext dbContext = new DatabaseContext())
{
var result = dbContext.Database.SqlQuery<int32>("exec DatabaseSchema.EmailAddressIsDuplicate {0}", emailAddress).FirstOrDefault();
}
Anything other 0 in the return value indicates there is a match and the number indicates the number of matches

How can I run a stored procedure that has no return values from C# with my db context?

I am using the following code to run a stored procedure:
MyContext db = new MyContext();
var a = db.Database.SqlQuery<string>("dbo.getQuestionUIdsByTopicId #TopicId",
new SqlParameter { ParameterName = "TopicId", Value = testTopicId });
This works good but now I will have a stored procedure that does not return any data other than a return code.
How can I execute a stored procedure with a parameter using my context db.Database and have the stored procedure return only a return code? If someone could give an example of a 3-4 line SP and how it returns a return code that would also be a great help.
You can use ExecuteSqlCommand to send non-query commands to the database.
int result = db.Database.ExecuteSqlCommand("exec sproc...");
See this link for more info.

Categories

Resources