I have a simple procedure in MySQL (same as in Retrieving values of function output/proc output parameters in JavaScript from ADODB commands to MySQL?).
As an attempt to get around the dead end of not being able to fetch the values of the output parameters, I started working on this from the aspx.cs component of a C# VisualStudio website project.
However, this method so far doesn't even execute the procedure, let alone return the values.
//MySQL Procedure - where user readonly has read and execute auth
CREATE DEFINER=`root`@`localhost` PROCEDURE `TEST`(OUT V_VAR VARCHAR(10))
BEGIN
DECLARE V_ID INT;
SELECT MAX(ID)+1 INTO V_ID FROM storedprocandfunccalllog;
INSERT INTO storedprocandfunccalllog VALUES(V_ID,"Test",NOW());
SELECT 'OUCH' INTO V_VAR FROM DUAL;
SELECT @V_VAR;
END
//Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = "Server=localhost; Database=mine; User=READONLY; Password=readonly; Port=3306";
MySqlCommand cmd = new MySqlCommand();
Response.Write("<script>alert('Here');</script>");
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "Call Test(@V_VAR);";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
cmd.Parameters.Add(new MySqlParameter("@V_VAR", "VarChar"));
cmd.Parameters["@V_VAR"].DbType = System.Data.DbType.String;
cmd.Parameters["@V_VAR"].Direction = ParameterDirection.Output;
Response.Write("<script>alert('Here');</script>");
Response.Write("<script>console.log('Command Test: ');</script>");
Response.Write("<script>console.log('Command Test: " + cmd.CommandText + "');console.log('Command Parameters: '); console.log('" + cmd.Parameters + "');</script>");
try
{
MySqlDataReader myReader;
myReader = cmd.ExecuteReader();
while (myReader.Read())
{ Console.WriteLine(myReader.GetString(0)); }
Console.WriteLine("Return Value: " + cmd.Parameters["@V_VAR"].Value);
HelloWorldLabel.Text = cmd.Parameters["@V_VAR"].Value.ToString();
Response.Write("<script>console.log('Here');</script>");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Response.Write("<script>alert('Error' );</script>");
Response.Write("<script>alert(\"Error: \"+"+ ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
Response.Write("<script>console.log(\"Error: \"+" + ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
}
conn.Close();
Response.Write("<script>alert('Done');</script>");
}
}
The expected result is 'Ouch' to populate to the screen, or in the console.log.
However what I get is:"Error: 1414 has occurred: OUT or INOUT argument 1 for routine mine.TEST is not a variable or NEW pseudo-variable in BEFORE trigger"
Swapping CommandType.Text with CommandType.StoreProcedure
cmd.CommandType = CommandType.StoredProcedure;
...results in:
Error: 0 has occurred: Procedure or function 'Call Test(@V_VAR)
' cannot be found in database 'mine
'.
And stripping the command text string down to just Test:
cmd.CommandText = "Test";
...results with an error page:
Data is Null. This method or property cannot be called on Null values.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
Source Error:
Line 45: {
Line 46: MySqlDataReader myReader;
--> Line 47: myReader = cmd.ExecuteReader();
**Through lots of trial and error, I've gotten functions to work - still need procedures.
Additional modifications...the user privileges in the database need to be more than SELECT and EXECUTE...what exactly, I don't know, but when I switch to an admin user with full privileges it works - the function and procedure calls work, but procedures are still not returning output values **
I have a MySQL function called ouch
:
CREATE DEFINER=`root`@`localhost` FUNCTION `OUCH`() RETURNS varchar(10) CHARSET utf8mb4
DETERMINISTIC
RETURN 'OUCH'
When I alter the apsx.cs file to the following:
cmd.CommandText = "Ouch";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add(new MySqlParameter("@V_VAR", MySqlDbType.VarChar));
cmd.Parameters["@V_VAR"].Direction = ParameterDirection.ReturnValue;
Response.Write("<script>alert('Here');</script>");
Response.Write("<script>console.log('Command Test: ');</script>");
Response.Write("<script>console.log('Command Test: " + cmd.CommandText + "');console.log('Command Parameters: '); console.log('" + cmd.Parameters + "');</script>");
try
{
cmd.ExecuteReader();
HelloWorldLabel.Text = cmd.Parameters["@V_VAR"].Value.ToString();
Response.Write("<script>console.log('" + cmd.Parameters["@V_VAR"].Value.ToString() + "');</script>");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Response.Write("<script>alert('Error' );</script>");
Response.Write("<script>alert(\"Error: \"+"+ ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
Response.Write("<script>console.log(\"Error: \"+" + ex.Number + "+\" has occurred: " + ex.Message + "\");</script>");
}
"OUCH" does populate to the screen and to the console.log. So I could use this for returning single values.
However, that doesn't answer why the procedures still won't return the output parameter.