Return Value from a Stored Procedure - c#

I am trying to get data returned from my stored procedure. This is what I have tried and absolutely nothing is coming back.
Stored Proc:
Declare #Submit_ID as int
Set #Submit_ID = (Select (Max(Sub_ID) + 1) from Sub_Details);
INSERT INTO.....
Return #Submit_ID
C# (2.0)
SqlConnection conn = null;
int subid;
try
{
conn = new SqlConnection(conn_string);
SqlCommand cmd = new SqlCommand("INSERT_INTO_MYDB", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#FirstName", SqlDbType.NVarChar, 255);
p1.Direction = ParameterDirection.Input;
p1.Value = txtFirstName.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("#LastName", SqlDbType.NVarChar, 255);
p2.Direction = ParameterDirection.Input;
p2.Value = txtLastName.Text;
cmd.Parameters.Add(p2);
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub);
}

You're not adding the return parameter to the command:
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(pSub); // <------------- add to command
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(cmd.Parameters["pSub"].Value);

First add parameter to cmd and use .Value to its value
cmd.Parameters.Add(pSub); // add parameters to command
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub.Value); // Use .Value

Related

Error: ORA-01652: unable to extend temp segment by 128 in tablespace TEMP

Good afternoon, I'm running a procedure and returning a cursor, so going the ORA-01652 error: unable to extend temp segment by 128 in tablespace TEMP
C# , ASP.NET
OracleCommand cmd = connection.CreateCommand();
OracleDataReader dr;
DataTable dt = new DataTable();
try
{
OpenConn();
cmd.CommandText = "UNITIZA_RELATORIOS.getComparaZila";
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter par1 = new OracleParameter("vElemServico", OracleType.VarChar);
par1.Value = elemServico;
par1.Direction = ParameterDirection.Input;
OracleParameter par2 = new OracleParameter("vdataInicio", OracleType.VarChar);
par2.Value = dataInicio;
par2.Direction = ParameterDirection.Input;
OracleParameter par3 = new OracleParameter("vdataFim", OracleType.VarChar);
par3.Value = dataFim;
par3.Direction = ParameterDirection.Input;
OracleParameter par4 = new OracleParameter("vOrdem", OracleType.VarChar);
par4.Value = ordem;
par4.Direction = ParameterDirection.Input;
OracleParameter par5 = new OracleParameter("vCodAneel", OracleType.VarChar);
par5.Value = codigoAneel;
par5.Direction = ParameterDirection.Input;
OracleParameter par6 = new OracleParameter("nCodigoError", OracleType.Number);
par6.Size = 1;
par6.Direction = ParameterDirection.Output;
OracleParameter par7 = new OracleParameter("return_value", OracleType.Cursor);
par7.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(par1);
cmd.Parameters.Add(par2);
cmd.Parameters.Add(par3);
cmd.Parameters.Add(par4);
cmd.Parameters.Add(par5);
cmd.Parameters.Add(par6);
cmd.Parameters.Add(par7);
dr = cmd.ExecuteReader();
dt.Load(dr);
return dt;
Note: The error only when acontence consultation over 1800 lines
This indicates your process is chewing up all the temporary tablespace...perhaps inefficient joins or query syntax. Have a look at the explain plan and see why it needs all that space. It maybe easier to optimize the query than the other solution:
From here
Create a new datafile by running the following command:
alter tablespace TABLE_SPACE_NAME add datafile 'D:\oracle\Oradata\TEMP04.dbf' size 2000M autoextend on;

Why is an output parameter is expected in this stored procedure?

I have written a simple stored procedure with some output parameters, but when I execute it from my C# code, I get an error
Expects Parameters
It is expecting me provide values for the OUTPUT parameters ...
Please help me figure out the error ...
My stored procedure:
ALTER PROCEDURE [dbo].[Check_EntryTrade_value]
#Account_ID bigint,
#Date datetime2(7),
#tradeby varchar(20),
#Symbol varchar(20),
#B_S varchar(20),
#RateE float ,
#Usd_Marg bigint OUTPUT,
#Amount bigint OUTPUT,
#Rate float OUTPUT,
#Entry_Order_id bigint OUTPUT
AS
BEGIN
SELECT
#Rate = [Rate],
#Entry_Order_id = [EntryOrder_ID],
#Amount = [Amount],
#Usd_Marg = [Usd_Mrg]
FROM
[dbo].[entrytable]
WHERE
[B_S] = #B_S
AND [Rate] = #RateE
AND [Account_ID] = #Account_ID
AND [Symbol] = #Symbol;
IF (#Rate IS NOT NULL)
INSERT INTO neworder (Account_ID, Symbol, B_S, Rate, Amount, Date, Usd_Marg, tradeby)
VALUES(#Account_ID, #Symbol, #B_S, #Rate, #Amount, #Date, #Usd_Marg, #tradeby);
DELETE FROM entrytable WHERE EntryOrder_ID = #Entry_Order_id;
END
And this is my C# Code:
SqlConnection conn = new SqlConnection(Properties.Settings.Default.MyCon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
//SQL INJECTION '"+ +"'
cmd.CommandText = "Check_EntryTrade_value";
SqlParameter pID1 = new SqlParameter("#Account_ID", SqlDbType.BigInt, 10);
pID1.Value = 1;
SqlParameter pID2 = new SqlParameter("#Symbol", SqlDbType.VarChar, 20);
pID2.Value = "EUR/USD";
SqlParameter pID3 = new SqlParameter("#B_S", SqlDbType.VarChar, 20);
pID3.Value = "Buy";
SqlParameter pID4 = new SqlParameter("#RateE", SqlDbType.Float, 10);
pID4.Value = 1.24763;
//SqlParameter pID6 = new SqlParameter("#Amount", SqlDbType.BigInt, 10);
//pID6.Value = 1;
SqlParameter pID5 = new SqlParameter("#Date", SqlDbType.DateTime2, 7);
pID5.Value = DateTime.Now.ToShortDateString();
SqlParameter pID6 = new SqlParameter("#tradeby", SqlDbType.VarChar, 20);
pID6.Value = "User";
cmd.Parameters.Add(pID1);
cmd.Parameters.Add(pID2);
cmd.Parameters.Add(pID3);
cmd.Parameters.Add(pID4);
cmd.Parameters.Add(pID5);
cmd.Parameters.Add(pID6);
cmd.ExecuteNonQuery();
Running my C# code returns this error:
Procedure or Function 'Check_EntryTrade_value' expects parameter '#Usd_Marg', which was not supplied.
I Found it :) .. Thanks Every One for Help .. i love StackOverFlow :P
Here is what i did :
//STEP-1 CONNECTION
SqlConnection conn = new SqlConnection(Properties.Settings.Default.MyCon);
//STEP-2 COmmand
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
//SQL INJECTION '"+ +"'
cmd.CommandText = "Check_EntryTrade_value";
SqlParameter pID1 = new SqlParameter("#Account_ID", SqlDbType.BigInt, 10);
pID1.Value = 1;
SqlParameter pID2 = new SqlParameter("#Symbol", SqlDbType.VarChar, 20);
pID2.Value = "EUR/USD";
SqlParameter pID3 = new SqlParameter("#B_S", SqlDbType.VarChar, 20);
pID3.Value = "Buy";
SqlParameter pID4 = new SqlParameter("#RateE", SqlDbType.Float, 10);
pID4.Value = 1.24763;
//SqlParameter pID6 = new SqlParameter("#Amount", SqlDbType.BigInt, 10);
//pID6.Value = 1;
SqlParameter pID5 = new SqlParameter("#Date", SqlDbType.DateTime2, 7);
pID5.Value = DateTime.Now.ToShortDateString();
SqlParameter pID6 = new SqlParameter("#tradeby", SqlDbType.VarChar, 20);
pID6.Value = "User";
SqlParameter pID7 = new SqlParameter("#Usd_Marg", SqlDbType.BigInt, 10);
pID7.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID8 = new SqlParameter("#Amount", SqlDbType.BigInt, 20);
pID8.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID9 = new SqlParameter("#Rate", SqlDbType.BigInt, 20);
pID9.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID10 = new SqlParameter("#Entry_Order_id", SqlDbType.BigInt, 20);
pID10.Direction = ParameterDirection.Output;
// pID6.Value = "User";
cmd.Parameters.Add(pID1);
cmd.Parameters.Add(pID2);
cmd.Parameters.Add(pID3);
cmd.Parameters.Add(pID4);
cmd.Parameters.Add(pID5);
cmd.Parameters.Add(pID6);
cmd.Parameters.Add(pID7);
cmd.Parameters.Add(pID8);
cmd.Parameters.Add(pID9);
cmd.Parameters.Add(pID10);
Try to remove
#Usd_Marg bigint OUTPUT,
#Amount bigint OUTPUT,
#Rate float OUTPUT,
#Entry_Order_id bigint OUTPUT
from stored procedure definition .
Modify your query :
SELECT [Rate], [EntryOrder_ID],[Amount],[Usd_Mrg]
FROM [dbo].[entrytable]
WHERE [B_S] = #B_S AND [Rate] = #RateE AND [Account_ID] = #Account_ID AND [Symbol] = #Symbol;
On the C# side you will receive a data set so you need to change last line of c# code :
cmd.ExecuteScalar();

sqlParameterCollection error

I have a definiton of procedure
public int Add_Nastavenie(out int typNastav, int nastavID, string hod)
{
ResetParameters();
cmd.CommandText = "add_Nastav";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParameter;
var sqlParameterOut = new SqlParameter("#TypNastav", SqlDbType.Int);
sqlParameterOut.Direction = ParameterDirection.Output;
sqlParameter = new SqlParameter("#NastavenieID", SqlDbType.Int);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.Value = nastavID;
cmd.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("#Hodnota", SqlDbType.NVarChar, 100);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.Value = hod;
cmd.Parameters.Add(sqlParameter);
var sqlParameterRet = new SqlParameter("retValue", SqlDbType.Int);
sqlParameterRet.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
typNastav = (int)sqlParameterOut.Value;
return (int)cmd.Parameters["retvalue"].Value;
}
I call this procedure like this :
dataConnector.Add_Nastavenie(out typNastav,nastavID,hod);
It's show error :
sql parameter with ParameterName 'retvalue' is not contained by this
sqlParameterCollection
How can i fix it ?
You are getting this because you haven't added the return parameter to command. you missed this
cmd.Parameters.Add(sqlParameterRet );
before
cmd.ExecuteNonQuery();

Allow Null values in parameterised SQL query?

I have the following SQL query that inserts the record into a database based on the parameters. It works well but does not allow null values, returning an error if there is a Null.
The table is set up to allow Nulls.
SqlConnection dbconnection = new SqlConnection(#"Data Source=LEWIS;Initial Catalog=CustomPC;Integrated Security=True");
SqlCommand InsertPC = new SqlCommand("INSERT INTO [Custom_PC] ([CPU_ID], [Motherboard_ID],[Graphics_Card_ID],[PSU_ID],[RAM_ID],[Case_ID],[Primary_Storage_Drive_ID],[Secondary_Storage_Drive_ID],[Primary_Optical_Drive_ID],[Secondary_Optical_Drive_ID]) VALUES (#CPU_ID, #Motherboard_ID, #Graphics_Card_ID, #PSU_ID, #RAM_ID, #Case_ID, #Primary_Storage_Drive_ID, #Secondary_Storage_Drive_ID, #Primary_Optical_Drive_ID, #Secondary_Optical_Drive_ID) SET #ReturnedID = Scope_Identity();", dbconnection);
SqlParameter CPU_IDParam = InsertPC.Parameters.Add("#CPU_ID", SqlDbType.Int);
CPU_IDParam.Value = Session["DATA"];
SqlParameter Motherboard_IDParam = InsertPC.Parameters.Add("#Motherboard_ID", SqlDbType.Int);
Motherboard_IDParam.Value = Session["MotherboardID"];
SqlParameter Graphics_Card_IDParam = InsertPC.Parameters.Add("#Graphics_Card_ID", SqlDbType.Int);
Graphics_Card_IDParam.Value = Session["GraphicsID"];
SqlParameter PSU_IDParam = InsertPC.Parameters.Add("#PSU_ID", SqlDbType.Int);
PSU_IDParam.Value = Session["PSUID"];
SqlParameter RAM_IDParam = InsertPC.Parameters.Add("#RAM_ID", SqlDbType.Int);
RAM_IDParam.Value = Session["RAMID"];
SqlParameter Case_IDParam = InsertPC.Parameters.Add("#Case_ID", SqlDbType.Int);
Case_IDParam.Value = Session["CaseID"];
SqlParameter Primary_Storage_Drive_IDParam = InsertPC.Parameters.Add("#Primary_Storage_Drive_ID", SqlDbType.Int);
Primary_Storage_Drive_IDParam.Value = Session["HDD1ID"];
SqlParameter Secondary_Storage_Drive_IDParam = InsertPC.Parameters.Add("#Secondary_Storage_Drive_ID", SqlDbType.Int);
Secondary_Storage_Drive_IDParam.Value = Session["HDD2ID"];
SqlParameter Primary_Optical_Drive_IDParam = InsertPC.Parameters.Add("#Primary_Optical_Drive_ID", SqlDbType.Int);
Primary_Optical_Drive_IDParam.Value = Session["OpticalDrive1ID"];
SqlParameter Secondary_Optical_Drive_IDParam = InsertPC.Parameters.Add("#Secondary_Optical_Drive_ID", SqlDbType.Int);
Secondary_Optical_Drive_IDParam.Value = Session["OpticalDrive2ID"];
SqlParameter ReturnedIDParam = InsertPC.Parameters.Add("#ReturnedID", SqlDbType.Int);
ReturnedIDParam.Direction = ParameterDirection.Output;
dbconnection.Open();
InsertPC.ExecuteNonQuery();
Session["PCNum"] = (ReturnedIDParam.Value);
Response.Redirect("~/CustomPC_Details.aspx");
You need to check whether the object is null and set your parameter to DBNull.Value.
SqlParameter Secondary_Optical_Drive_IDParam = InsertPC.Parameters.Add("#Secondary_Optical_Drive_ID", SqlDbType.Int);
Secondary_Optical_Drive_IDParam.Value = (object)Session["OpticalDrive2ID"] ?? (object)DBNull.Value;
Note that you need to cast to object because with the ?? operator both possible values must have the same type.

Prevent users from registering twice

I have a register form and I want to check that the user did not register before.
My code is in below. I think two problems exist: (1) the call method and (2) passing the parameter to the stored procedure. My symptom is that this causes an exception that says the input parameter not initialized.
create procedure fakeuser #username nvarchar(250),#codemeli nchar(10),#email nvarchar(50), #user nvarchar(250) output,#code nchar(10)output,#mail nvarchar(50)output
as
if exists(select username,email,codemeli from karbar where username=#username)
set #user=#username
else if exists(select username,email,codemeli from karbar where codemeli=#codemeli)
set #code=#codemeli
else if exists(select username,email,codemeli from karbar where email=#email)
set #mail= #email
Here is the c# code:
public static string confirm(string username, string email, string codemeli)
{
string constring = "data source=.;database=site;integrated security=true;";
SqlConnection connection = new SqlConnection(constring);
// Command - specify as StoredProcedure
SqlCommand command = new SqlCommand("fakeuser", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("#username", SqlDbType.NVarChar);
param.Direction = ParameterDirection.Input;
param.Value = username;
command.Parameters.Add(param);
SqlParameter param2 = new SqlParameter("#email", SqlDbType.NVarChar);
param2.Direction = ParameterDirection.Input;
param2.Value = username;
command.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("#codemeli", SqlDbType.NChar);
param3.Direction = ParameterDirection.Input;
param3.Value = username;
command.Parameters.Add(param3);
// Return value as parameter
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar);
returnuser.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnuser);
SqlParameter returncode = new SqlParameter("#code", SqlDbType.NChar);
returncode.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returncode);
SqlParameter returnmail = new SqlParameter("#mail", SqlDbType.NVarChar);
returnmail.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnmail);
// Execute the stored procedure
connection.Open();
command.ExecuteNonQuery();
connection.Close();
Thank you.
Set ParameterDirection=Output for #user,#code, and #mail and also specify the width/size/length of parameter.
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar,100);
returnuser.Direction = ParameterDirection.Output;
command.Parameters.Add(returnuser);

Categories