2

I need verify the Parameter RGP, PEIXE, DATA_REGISTRO for my method return true. But on PEIXE parameter this giving error:

Expects 'PEIXE' parameter, which was not provided.

PEIXE is a Varchar type on SQL, RGP is a Int type and DATA_REGISTRO is a Date type.

 public bool Search_RGP_Cadastro(int param_RGP, string param_date, string param_peixe)
    {
        SqlDataReader objReader;
        SqlCommand objcmd = null;

        vsql = "SELECT [RGP], [PEIXE], [PESO], [QUANTIDADE], [DATA_REGISTRO] FROM cadastro WHERE RGP = @RGP and PEIXE = @PEIXE and DATA_REGISTRO = @DATA_REGISTRO";

        if (this.Conectar())
        {
            try
            {
                DateTime dtParam = DateTime.Parse(param_date);

                objcmd = new SqlCommand(vsql, objCon);

                objcmd.Parameters.Add(new SqlParameter("@RGP", param_RGP));
                objcmd.Parameters.Add(new SqlParameter("@PEIXE", param_peixe));
                objcmd.Parameters.Add(new SqlParameter("@DATA_REGISTRO", dtParam));

                objReader = objcmd.ExecuteReader();

                if (objReader.Read())
                {
                    valor.retorna_RGP = objReader.GetInt32(0);
                    valor.retorna_nome_peixe = objReader.GetString(1);
                    valor.retorna_peso = objReader.GetDouble(2);
                    valor.retorna_Quantidade = objReader.GetInt32(3);
                    valor.retorna_date_time = objReader.GetDateTime(4);
                }
                return true;
            }
            catch
            {
                throw;
            }
            finally
            {
                this.Desconectar();
            }


        }
        else
            return false;


    }
1
  • You should not re-use SQL connections. ADO.NET has built in Connection Pooling, you should declare your SqlConnection objects in the scope you need them inside using blocks. As long as you pass in the same connection string a 2nd SqlConnection can re-use the connection from a previously disposed object. Commented Jul 22, 2015 at 17:07

1 Answer 1

1

I'd wrap your param_peixe in single quotes, like this:

objcmd.Parameters.Add(new SqlParameter("@PEIXE", "'" + param_peixe + "'"));

It wouldn't be the first time I've seen SQL reject the use of a string as a VARCHAR.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok its worth. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.