I have a SQL stored procedure for updating my table. But when executing the query via C#, xslt the one of the columns deleted from the table.
My stored procedure is
ALTER PROCEDURE [dbo].[kt_editingnotes]
(#NOTE NVARCHAR (512),
#ITEMNUMBER NVARCHAR (512),
#ACCOUNT NVARCHAR (512),
#CODE NVARCHAR(512)
)
AS
UPDATE NOTES
SET TXT = #NOTE
WHERE NOTESRECID = (SELECT ISP_EFAVORITLINE.ROWNUMBER
FROM ISP_EFAVORITLINE
WHERE ISP_EFAVORITLINE.ACCOUNT = #ACCOUNT
AND ISP_EFAVORITLINE.ITEMNUMBER = #ITEMNUMBER
AND ISP_EFAVORITLINE.CODE = #CODE)
return
and I am calling it like this:
ExecStoredProcedure('kt_editingnotes', concat('#ITEMNUMBER:', $ITEMNUMBER,', #ACCOUNT:', $ACCOUNT,', #CODE:', $CODE))
What is the problem? Can anyone help?
editingI noticed in the ExecStoredProcedure that you execute the procedure 'kt_deletenotes' instead of 'kt_editingnotes'. Try changing the line where you call your procedure to call the correct procedure.
ExecStoredProcedure('kt_editingnotes', concat('#ITEMNUMBER:', $ITEMNUMBER,', #ACCOUNT:', $ACCOUNT,', #CODE:', $CODE))
Related
I have connected a stored procedure to a C# program using Entity Framework with a .edmx model. I am trying to get an integer output value to a variable from the stored procedure which was connected to entity. How can I get the output result from the stored procedure to the local variable?
Here is the code in C#
_db = new DbEntities();
int id = 0;
var fleetid = _db.GetNextFleetId(id);
SQL Server stored procedure:
PROCEDURE dbo.GetNextFleetId
#NewId bigint OUTPUT
AS
BEGIN
SELECT #NewId = NEXT VALUE FOR dbo.seqFleets;
RETURN;
END
If we add stored procedure to edmx model from the database.
we can try to use ObjectParameter to get output value from stored procedure.
ObjectParameter outPutId = new ObjectParameter("NewId", typeof(long));
_db.GetNextFleetId(outPutId);
//output value
Convert.ToInt64(outPutId.Value);
Currently, I'm working on a project for a shop. The problem I encountered is that the "INSERT" query didn't actually insert anything.
The try-catch didn't find anything wrong, which is adding my confusion. I have also debugged the code and found nothing abnormal.
koneksi.Open();
MySqlCommand masuk = new MySqlCommand(a, koneksi);
masuk.CommandType = CommandType.StoredProcedure;
Random utk_id = new Random();
int panggil_acak = utk_id.Next();
masuk.Parameters.AddWithValue("_id_transaksi", Convert.ToString(panggil_acak));
masuk.Parameters.AddWithValue("_id_barang", Convert.ToString(id.Text));
masuk.Parameters.AddWithValue("_nama_barang", Convert.ToString(nama.Text));
masuk.Parameters.AddWithValue("_harga_satuan", Convert.ToString(harga.Text));
masuk.Parameters.AddWithValue("_jumlah_barang", Convert.ToString(jmlh_brg_beli.Text));
masuk.Parameters.AddWithValue("_diskon", Convert.ToString(diskon_rp.Text));
masuk.Parameters.AddWithValue("_harga_total", Convert.ToString(harga_barang_total.Text));
masuk.Parameters.AddWithValue("_tgl_transaksi", Convert.ToString(DateTime.Now.ToString()));
//execute command
masuk.ExecuteNonQuery();
clear text area and refresh data grid
rstArea();
isiDgrid();
the command is a stored procedure and the application gives no error feedback.
CREATE DEFINER=`root`#`localhost` PROCEDURE `belanjaTemp_add`(
_id_transaksi varchar(20),
_id_barang varchar(10),
_nama_barang varchar(255),
_harga_satuan varchar(10),
_jumlah_barang varchar(10),
_diskon varchar(45),
_harga_total varchar(10),
_tgl_transaksi varchar(45)
)
BEGIN
if(_id_transaksi = 0)
then
insert into komodo.penjualan_temp
(id_transaksi, id_barang, nama_barang, harga_satuan, jumlah_barang, diskon, harga_total, tgl_transaksi)
values
(_id_transaksi, _id_barang, _nama_barang, _harga_satuan, _jumlah_barang, _diskon, _harga_total, _tgl_transaksi);
END if;
END
I expected the database will be updated but it's not updated.
The screenshot for succeed confirmation :
https://ibb.co/b26YdBY
My question : how do I manage to get the data inserted?
Try using ExecuteStoredProcedure() instead of ExecuteNonQuery();
I am trying to call a stored procedure using C# EF6 to bring back data. I have tried to run the stored procedure in SQL management studio and it seems to work fine, however when I try to run it in my application I get an error saying "Must declare the scalar variable "#devID"
Here is part of my method in my application calling the stored procedure
public IHttpActionResult GetMetrics(int deviceID, string attribute, string startDate)
{
if (deviceID == 0)
{
return NotFound();
}
var metrics = db.Database.SqlQuery<Metrics>("GetMetrics #devID, #MetricType, #startTime", deviceID, attribute, startDate).ToList();
and here is my stored procedure:
ALTER PROCEDURE [dbo].[GetMetrics]
-- Add the parameters for the stored procedure here
#devID int,
#MetricType nvarchar(20),
#startTime nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT *
FROM dbMetrics
WHERE deviceID = #devID and MetricType = #MetricType and timeStamp >= #startTime
ORDER BY timeStamp
END
As per the documentation, if you want to use named parameters, you need to pass SqlParameter objects like this:
var metrics = db.Database.SqlQuery<Metrics>("GetMetrics #devID, #MetricType, #startTime",
new SqlParameter("devID", deviceID),
new SqlParameter("MetricType", attribute),
new SqlParameter("startTime", startDate)
).ToList();
alter procedure [dbo].[XXX]
(
#vendorworksationID uniqueidentifier ,
#sdate date,
#edate date,
#total int out
)
begin
select #total = COUNT(*)
from AdvertisedCampaignHistory a
where
CAST(a.CreationDate AS DATE) BETWEEN CAST(#sdate as DATE) AND CAST(#edate as DATE)
and a.CampaignID in (select cc.BCampaignID
from BeaconCampaign cc, VendorWorkStation vw
where cc.VendorWorkStationID = vw.VendorWorkStationID
and VendorID = #vendorworksationID)
return #total
end
The above code shows the stored procedure that return an integer value from SQL Server
ObjectParameter Output = new ObjectParameter("total", typeof(Int32));
var resBC = this.Context.getTotalSentBeaconCampaign(VendorWorkstationID, sdate,edate,Output).FirstOrDefault();
The above code shows how I am passing parameters and retrieving the value on the C# side
While running the code I am getting following error
The data reader returned by the store data provider does not have
enough columns for the query requested.
What could be the possible cause for this error?
Entity Framework cannot support Stored Procedure Return scalar values out of the box.To get this to work with Entity Framework, you need to use "Select" instead of "Return" to return back the value.
More Ref : http://www.devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values
I have a SQL stored procedure and I need to be able to pass a NULL as a value to one of its parameters to be used in a query like this:
create procedure sp
#param1 varchar(30)
as
select * from table where field = isnull(#param1, field)
So I need to some how tell EF to make #param1 nullable. How do I do this?
Thanks!
In case it helps, the process I use with EF is:
Create SP
Update Model (edmx)
Add new function import
Generate new complex type by clicking the button
Run Custom Tool on separate template file (to generate POCO)
As a work-around, you could declare two separate stored procedures:
-- use this for non-null parameters
create procedure sp
#param1 varchar(30)
as
select * from table where field = #param1
-- use this for null
create procedure sp_null
as
select * from table
and then you can write the desired abstraction in C#:
public ... GetSp(string param1)
{
if (param1 == null)
return ....sp_null();
else
return ....sp(param1);
}
Quick look and I found this on stackoverflow. Hope it helps.
Entity Framework 4.0 Entity SQL passing null ObjectParameter parameters
Use DBNull.Value, I've done exactly this with one of my stored procedures. To call your procedure my code would look like:
List<ObjectParameter> objectParameterList = new List<ObjectParameter>();
if (string.IsNullOrEmpty(param1))
{
object nullValue = DBNull.Value;
objectParameterList.Add(new ObjectParameter("param1", nullValue));
}
else
{
objectParameterList.Add(new ObjectParameter("param1", param1));
}
context.ExecuteFunction("MyEFModel.sp", objectParameterList.ToArray());
Hopefully this helps.
set the default value in Stored procedure as NULL.
create procedure sp
#param1 varchar(30) =NULL
as
select * from table where field = isnull(#param1, field)