I'm working with stored procedures for the first time. I'm facing hard time figuring out problem behind this. If I execute stored procedure from PhpMyAdmin manually, it works fine. But when I use it with C# program. It shows error.
Stored Procedure:
CREATE DEFINER=`root`@`localhost` PROCEDURE `Save`(INOUT `id` INT, IN `cId` INT, IN `date` DATE, IN `randomNum` INT, IN `debitAmount` VARCHAR(20), IN `descr` VARCHAR(255))
NO SQL
BEGIN
INSERT INTO dc (status, dcDate, clientId) VALUES ('1', date, cId);
SET id = last_insert_id();
INSERT INTO bill (dcId, clientId, billDate, referenceNo) VALUES (
id,
clientId,
date,
randomNum
);
INSERT INTO ledger (clientId, debit, ledgerDate, description, dcId, referenceNo)
VALUES (
cId,
debitAmount,
date,
descr,
id,
randomNum
);
SELECT @id;
END
When I execute this procedure with these parameters:
var reader = connection.StoredProcedureInOut("Save", new String[] { "0", theDate, clientId, randomNumber.ToString(), totalDebit.ToString(), description }, new String[] { "id", "date", "cId","randomNum", "debitAmount", "descr" });
Definition is:
public MySqlDataReader StoredProcedureInOut(String procedureName, String[] values, String[] keys)
{
openConnection();
MySqlDataReader reader;
try
{
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < values.Length; i++)
{
cmd.Parameters.AddWithValue("@" + keys[i], values[i]);
}
reader = cmd.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
reader = null;
}
return reader;
}
I get this message:
OUT or INOUT argument 1 for routine noorani.Save is not a variable or NEW pseduo-variable in Before trigger
Resolved the problem by altering the code like this:
var reader = connection.StoredProcedureInOut("Save", new String[] {theDate, clientId, randomNumber.ToString(), totalDebit.ToString(), description }, new String[] {"date", "cId","randomNum", "debitAmount", "descr" }, new String[] {"id"});
Definition:
public String[] StoredProcedureInOut(String procedureName, String[] values, String[] keys, String[] outputParameters)
{
openConnection();
String[] newlyInsertedID = new String[outputParameters.Length];
try
{
MySqlCommand cmd = new MySqlCommand(procedureName, connection);
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < outputParameters.Length; i++)
{
cmd.Parameters.AddWithValue("@" + outputParameters[i], "0");
var parameterName = "@"+outputParameters[i];
cmd.Parameters[parameterName].Direction = ParameterDirection.Output;
}
for (int i = 0; i < values.Length; i++)
{
cmd.Parameters.AddWithValue("@" + keys[i], values[i]);
var parameterName = "@" + keys[i];
cmd.Parameters[parameterName].Direction = ParameterDirection.Input;
}
//reader = cmd.ExecuteReader();
cmd.ExecuteNonQuery();
for(int i = 0; i < outputParameters.Length; i++)
{
var parameterName = "@"+outputParameters[i];
newlyInsertedID[i] = cmd.Parameters[parameterName].Value.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//reader = null;
}
closeConnection();
return newlyInsertedID;
}
Thing which was missing in the code was Direction . Moreover this is a generic code can be executed for all types of Out/InOut stored Procedures.
Hope this is helpful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.