HernanBogantes 0 Newbie Poster

Hi,

I really need your help. And I really will appreciate it also.

By using ASP.NET I need to show in a Label or in a gridview control the output parameter of an “Stored Procedure”

ERROR: Parameter 'RESULTADO' not found in the collection.

PLEASE Help

‘****************************************************************************************
DELIMITER $$
DROP PROCEDURE IF EXISTS `nombre` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `nombre`(
IN condicion VARCHAR(10),
OUT RESULTADO VARCHAR (10))
BEGIN
SELECT nombre
INTO RESULTADO
FROM franja
where topico = condicion;
END $$
DELIMITER ;
‘****************************************************************************************
‘****************************************************************************************
Dim hilera As String = "Server=localhost;Database=parrilla_comercial;Uid= root;Pwd= conectar;"
Dim conexion As MySqlConnection = New MySqlConnection(hilera)
Try
conexion.Open()
Dim comando As MySqlCommand = New MySqlCommand()
comando.CommandType = CommandType.StoredProcedure
comando.CommandText = "nombre"
comando.Connection = conexion

Dim p As MySqlParameter = New MySqlParameter()
p.MySqlDbType = SqlDbType.NVarChar
p.Value = TextBox1.Text
p.Direction = ParameterDirection.Input
p.ParameterName = "@condicion"
comando.Parameters.Add(p)
comando.ExecuteScalar()

Dim r As MySqlParameter = New MySqlParameter()
r.Direction = ParameterDirection.Output
r.ParameterName = "@RESULTADO"
comando.Parameters.Add(r)
comando.ExecuteScalar()
Label2.Text = comando.Parameters("@RESULTADO").Value.ToString
Catch ex As Exception
Label1.Text = ex.Message
Finally
If conexion.State = ConnectionState.Open Then
conexion.Close()
End If
End Try
‘****************************************************************************************

Dani AI

Generated

— the symptom you posted usually comes from one or more of these mistakes: adding the OUT parameter after executing the command, using the SQL Server enum instead of the MySQL enum/type, not giving a size for a string output parameter, or a mismatch in the exact parameter name used when adding vs reading it. Fix those and the parameter will be available after execution.

Do this sequence: create the command, set CommandType = StoredProcedure, add the input parameter and the output parameter (set its Direction and Size for strings), then call ExecuteNonQuery once and read the output parameter value. Use the MySQL parameter type (or DbType) — do not assign a SqlDbType value to a MySqlParameter. Also be consistent with the parameter name you use when adding and when reading it (use the same string; some MySQL providers normalize the leading '@' or '?' so try the same form both times).

A minimal VB.NET pattern (adjust names/types/sizes to your procedure) looks like this:

Using cmd As New MySqlCommand("nombre", conn)
    cmd.CommandType = CommandType.StoredProcedure

    cmd.Parameters.Add("condicion", MySqlDbType.VarChar).Value = TextBox1.Text
    Dim outp As MySqlParameter = cmd.Parameters.Add("RESULTADO", MySqlDbType.VarChar, 10)
    outp.Direction = ParameterDirection.Output

    cmd.ExecuteNonQuery()
    Label2.Text = Convert.ToString(outp.Value)
End Using

If problems persist, verify the stored procedure directly in MySQL client, dump the names/types in cmd.Parameters right before execution to confirm what the driver has registered, and consult the Connector/NET stored-procedures guide for provider-specific quirks: MySQL Connector/NET — Stored Procedures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.