I want to get the id of the last inserted row from mysql, but i get somekind of Exception unhandled. The visual studio gives me this error :
MySql.Data.MySqlClient.MySqlException: 'OUT or INOUT argument 9 for routine test.pr_VendegFelvetele is not a variable or NEW pseudo-variable in BEFORE trigger'.
mysql code:
CREATE PROCEDURE pr_VendegFelvetele(
IN uvezNev VARCHAR(75),
IN ukerNev VARCHAR(75),
IN uemail VARCHAR(255),
IN utel INT(15),
IN ucim VARCHAR(75),
IN uiranyito INT(5),
IN uvaros VARCHAR(55),
IN uorszag VARCHAR(45),
OUT uvendegID INT(11)
)
BEGIN
INSERT INTO `vendeg`(`vezNev`, `kerNev`, `email`, `tel`, `cim`, `iranyito`, `varos`, `orszag`)
VALUES (uvezNev, ukerNev, uemail, utel, ucim, uiranyito, uvaros, uorszag);
SET uvendegID = LAST_INSERT_ID();
END
By the way mysql procedure works perfectly.
c# code:
String query = "CALL pr_VendegFelvetele(#uvezNev, #ukerNev, #uemail, #utel, #ucim, #uiranyito, #uvaros, #uorszag, #uvendegID);";
MySqlCommand cmd = new MySqlCommand(query, Con);
cmd.Parameters.AddWithValue("#uvezNev", uvezNev);
cmd.Parameters["#uvezNev"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#ukerNev", ukerNev);
cmd.Parameters["#ukerNev"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#uemail", uemail);
cmd.Parameters["#uemail"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#utel", utel);
cmd.Parameters["#utel"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#ucim", ucim);
cmd.Parameters["#ucim"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#uiranyito", uiranyito);
cmd.Parameters["#uiranyito"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#uvaros", uvaros);
cmd.Parameters["#uvaros"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#uorszag", uorszag);
cmd.Parameters["#uorszag"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#uvendegID", MySqlDbType.Int32);
cmd.Parameters["#uvendegID"].Direction = ParameterDirection.Output;
Con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show(cmd.Parameters["#uvendegID"].Value.ToString());
I get it on cmd.ExecuteNonQuery();
I added these lines and now it works.
cmd.CommandText = "pr_VendegFelvetele";
cmd.CommandType = CommandType.StoredProcedure;
This link helped:
https://dev.mysql.com/doc/connector-net/en/connector-net-programming-stored-using.html
Related
Hi I am trying to call an as400 stored procedure using OleDB. Could you please post an example of how to do it cause I've been following some tutorials but not matter what I do I always get an Invalid Token Exception
this is what I do
OleDbCommand sp = new OleDbCommand("CALL NASVARWG.SP001(?,?,?,?,?) ", connectionDB);
sp.CommandType = CommandType.StoredProcedure;
sp.Parameters.Add("P1", System.Data.OleDb.OleDbType.Char).Value = "ESANASTRIS";
sp.Parameters["P1"].Size = 10;
sp.Parameters["P1"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P2", System.Data.OleDb.OleDbType.Char).Value = "SAMNAS";
sp.Parameters["P2"].Size = 10;
sp.Parameters["P2"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P3", System.Data.OleDb.OleDbType.Char).Value = textBox_Reparto.Text;
sp.Parameters["P3"].Size = 6;
sp.Parameters["P3"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P4", System.Data.OleDb.OleDbType.Char).Value = "we can do this";
sp.Parameters["P4"].Size = 60;
sp.Parameters["P4"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P5", System.Data.OleDb.OleDbType.Char).Value = "help";
sp.Parameters["P5"].Size = 256;
sp.Parameters["P5"].Direction = ParameterDirection.Input;
sp.Prepare();
sp.ExecuteNonQuery();
the exception I get says "NASVARWG" is not a valid token. Why? that is the name of the library containing the procedure and the spelling is correct.
Thanks for your help
A C# code with CommandType.StoredProcedure example :
// assume a DB2Connection conn
DB2Transaction trans = conn.BeginTransaction();
DB2Command cmd = conn.CreateCommand();
String procName = "INOUT_PARAM";
cmd.Transaction = trans;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
A C# code with CommandType.Text example :
// assume a DB2Connection conn
DB2Transaction trans = conn.BeginTransaction();
DB2Command cmd = conn.CreateCommand();
String procName = "INOUT_PARAM";
String procCall = "CALL INOUT_PARAM (#param1, #param2, #param3)";
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;
cmd.CommandText = procCall;
// Register input-output and output parameters for the DB2Command
cmd.Parameters.Add( new DB2Parameter("#param1", "Value1");
cmd.Parameters.Add( new DB2Parameter("#param2", "Value2");
DB2Parameter param3 = new DB2Parameter("#param3", IfxType.Integer);
param3.Direction = ParameterDirection.Output;
cmd.Parameters.Add( param3 );
// Call the stored procedure
Console.WriteLine(" Call stored procedure named " + procName);
cmd.ExecuteNonQuery();
// Register input-output and output parameters for the DB2Command
...
// Call the stored procedure
Console.WriteLine(" Call stored procedure named " + procName);
cmd.ExecuteNonQuery();
I am coding winform application where i call procedure in my datagrid.
I have method where I define parameters 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;
}
then i call procedure this way
dataConnector.Add_Nastavenie(typNastav,nastavID,hod);
I have an error Argument 1 must be passed with the 'out' keyword
I change it to dataConnector.Add_Nastavenie(out typNastav,nastavID,hod);
error dissapear but application is not working, procedure do nothing .
My try catch exception show : Procedure or function 'add_Nastav' expects parameter '#TypNastav', which was not supplied.
Can somebody help find solution ? Thanks .
You haven't added the OUTPUT parameter the stored procedure is expecting:
cmd.Parameters.Add(sqlParameterOut);
Example:
SqlCommand com = new SqlCommand("update_outptu_Stock", connect.con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#cg_id ", 1);
SqlParameter par = new SqlParameter("#id", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
SQL:
Create proc update_outptu_Stock
#cg_id int,
#id int output
as
begin
insert into example(cg_id) values (#cg_id)
set #id = SCOPE_IDENTITY() -- get value of id created
end
Go
Get parameter id:
var parvalue = id.value;
I am inserting rows in sql table through my c# code , which calls a Stored procedure .
C# code:
SqlCommand myCommand = thisConnection.CreateCommand();
myCommand.CommandText = "FederationUpdateCTRAndImpressionCountsForAllYPIds";
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("#bid", SqlDbType.UniqueIdentifier);
myCommand.Parameters.Add("#uid", SqlDbType.UniqueIdentifier);
myCommand.Parameters.Add("#imp", SqlDbType.VarChar);
myCommand.Parameters.Add("#ctr", SqlDbType.VarChar);
while (myfederationReader.Read())
{
myCommand.Parameters["#bid"].Value = myfederationReader["BusinessId"];
myCommand.Parameters["#uid"].Value = myfederationReader["UId"];
myCommand.Parameters["#imp"].Value = myfederationReader["Impression"];
myCommand.Parameters["#ctr"].Value = myfederationReader["CTR"];
rowsAffected = myCommand.ExecuteNonQuery();
}
Stored proc:
CREATE PROCEDURE [dbo].[FederationUpdateCTRAndImpressionCountsForAllYPIds]
#bid uniqueidentifier,
#uid uniqueidentifier,
#imp varchar(255),
#ctr varchar(255)
AS BEGIN
UPDATE BasicBusinessInformation
SET BasicBusinessInformation.CTR = #ctr , BasicBusinessInformation.Impression = #imp
WHERE BasicBusinessInformation.BusinessId = #bid AND BasicBusinessInformation.UId = #uid
END
On executing it , following error is reported:
procedure has no parameters and arguments were supplied
Try clearing the Command parametres
[C#]
public bool ExportAndClear() {
SqlParameter[] myParamArray = new SqlParameter[myCmd.Parameters.Count - 1];
myCmd.Parameters.CopyTo(myParamArray, 0);
myCmd.Parameters.Clear();
return true;
}
try to fetch data from reader before you fire this command
like
1)Fetch data from reader and store in list or datatable
2)For loop on list or datatable
3)in for loop fire this command
Try moving the following
SqlCommand myCommand = thisConnection.CreateCommand();
myCommand.CommandText = "FederationUpdateCTRAndImpressionCountsForAllYPIds";
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("#bid", SqlDbType.UniqueIdentifier);
myCommand.Parameters.Add("#uid", SqlDbType.UniqueIdentifier);
myCommand.Parameters.Add("#imp", SqlDbType.VarChar);
myCommand.Parameters.Add("#ctr", SqlDbType.VarChar);
In the while loop, see if you get different results.
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);
I have an existing stored procedure in SQL Server that I need to call from my C# code and get result. Here is how this SP looks like
CREATE PROCEDURE [dbo].[sp_MSnextID_DDB_NextID]
#tablesequence varchar(40)
AS
declare #next_id integer
begin transaction
update DDB_NextID
set DDB_SEQUENCE = DDB_SEQUENCE + 1
where DDB_TABLE = #tablesequence
select #next_id = DDB_SEQUENCE from DDB_NextID
where DDB_TABLE = #tablesequence
commit transaction
RETURN #next_id
Here is my C# code
using (OdbcConnection connection = new OdbcConnection(GetConnectionString()))
{
using (IDbCommand command = new OdbcCommand())
{
command.CommandText = "sp_MSnextID_DDB_NEXTID";
command.CommandType = CommandType.StoredProcedure;
IDbDataParameter parameter1 = command.CreateParameter();
parameter1.DbType = DbType.String;
parameter1.ParameterName = "#tablesequence";
parameter1.Value = "ddb_dc_document";
parameter1.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter1);
IDbDataParameter parameter2 = command.CreateParameter();
parameter2.DbType = DbType.Int32;
parameter2.ParameterName = "#Return_Value";
parameter2.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(parameter2);
command.Connection = connection;
connection.Open();
command.ExecuteNonQuery();
IDbDataParameter o = (command.Parameters)["#Return_Value"] as IDbDataParameter;
//Got return value from SP in o.Value
}
}
The trouble is I am getting exception
[42000] [Microsoft][SQL Native Client][SQL Server]Procedure or function 'sp_MSnextID_DDB_NextID' expects parameter '#tablesequence',
which was not supplied.
which I can't explain or fix. What I am missing?
To find a way around, I was trying different approach: executing the following query that sets data in a temp table
create table #temp (i integer); insert into #temp exec sp_MSNextID_DDB_NEXTID #tablesequence='ddb_dc_document';select * from #temp;
In this case SP is executed correctly but select returns zero rows!
Unfortunately, you can't use named parameters with OdbcCommand. You will need to instead execute a call statement to your stored procedure.
using (OdbcConnection connection = new OdbcConnection(GetConnectionString()))
{
using (IDbCommand command = new OdbcCommand())
{
command.CommandText = "{ ? = CALL sp_MSnextID_DDB_NEXTID(?) }";
command.CommandType = CommandType.StoredProcedure;
IDbDataParameter parameter2 = command.CreateParameter();
parameter2.DbType = DbType.Int32;
parameter2.ParameterName = "#Return_Value";
parameter2.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(parameter2);
IDbDataParameter parameter1 = command.CreateParameter();
parameter1.DbType = DbType.String;
parameter1.ParameterName = "#tablesequence";
parameter1.Value = "ddb_dc_document";
parameter1.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter1);
command.Connection = connection;
connection.Open();
command.ExecuteNonQuery();
IDbDataParameter o = (command.Parameters)["#Return_Value"] as IDbDataParameter;
//Got return value from SP in o.Value
}
}
To make your workaround work you should replace
RETURN #next_id
in your procedure with
SELECT #next_id