Error: "Return should not be followed by an object expression"

Asked

Viewed 214 times

4

I have this function that has a simple registration but is not registering

My doubt would be on that return RES; which gives the error message below the code.

//botao para cadastrar OS
        private void Button1_Click(object sender, EventArgs e)
        {
            bool res = false;

            try
            {              
                SqlCommand query = 
                    new SqlCommand("INSERTO INTO gerarOS VALUES (@codOS,@nomeCliente, @modeloMoto, @quilometragem, @dataOS)");

                query.Parameters.AddWithValue("@nomeOS", codOS);
                query.Parameters.AddWithValue("@nomeCliente", nomeCliente);
                query.Parameters.AddWithValue("@nomeOS", modeloMoto);
                query.Parameters.AddWithValue("@nomeOS", quilometragem);
                query.Parameters.AddWithValue("@nomeOS", dataOS);
                query.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //caso der erro na inserção
                res = false;
            }

            if (conexao.State == ConnectionState.Open)
                conexao.Close(); // fecha conexão

            return res;
        }

Gravity Code Description Project File Line Deletion State CS0127 Error Like "Geraros.Button1_click(Object, Eventargs)" returns void, a Return keyword should not be followed by an expression of Object Projectproject D: Programming Projectproject Geraros.cs66Ativo

  • Two things: 1 - Where is the code that opens the connection because without it I can’t fix the problem with the SqlCommand that does not save. 2 - Take the line return res for Button1_Click is void.

3 answers

4

friend, check these lines if they are indeed correct

            query.Parameters.AddWithValue("@nomeOS", codOS);
            query.Parameters.AddWithValue("@nomeCliente", nomeCliente);
            query.Parameters.AddWithValue("@nomeOS", modeloMoto);
            query.Parameters.AddWithValue("@nomeOS", quilometragem);
            query.Parameters.AddWithValue("@nomeOS", dataOS);

because although you are passing the right variables, the query data reference is wrong, the right one would be:

            query.Parameters.AddWithValue("@codOS", codOS);
            query.Parameters.AddWithValue("@nomeCliente", nomeCliente);
            query.Parameters.AddWithValue("@modeloMoto", modeloMoto);
            query.Parameters.AddWithValue("@quilometragem", quilometragem);
            query.Parameters.AddWithValue("@dataOS", dataOS);

check whether this solves, and I believe that if it does not, this error would also occur...

4

There are some possible solutions.

One of them is to take the res of return, so it conforms to the signature of the method that has a void and so must return nothing.

Another solution would be to change the method signature to allow returning the type of res, therefore a bool. But I guess you can’t because the event method requires a signature the way it’s there.

A different solution is to separate the logic from the click of the business logic button that manipulates the database, which is an excellent idea regardless of the error you are having (but this is another matter of design application), so you can have the signature of the method the way you want and can be a bool. Of course you will have to know where to use this method. I imagine there is somewhere that is already waiting to use this, otherwise it is more lost than you imagine and this error is not very important since the application is wrong much more widely.

A possible solution of having a value indicating whether it worked is to have a field in the object that is a bool and you keep the value there instead of returning in the method, but this idea seems quite wrong.

There are other problems in this code, although it works, if it works (I don’t think).

If you want to know something more than this error leave for another question, one problem at a time.

  • There’s something else wrong with the code, the SqlCommand.Connection is void.

  • I wrote that there are several errors in the code,, there are only 2 or 3. I only dealt with what he was more specific to what he demonstrated in the question.

4

Take a look around here, if you search for the error code you find the explanation, problem, in a "void" function can not return anything, you are returning a boolean.

It will be rather this.... check on this page the Sqlcommand documentation. It will be missing immediately before Executenonquery, the open Connection command, command.Connection.Open();

Add an example of the documentation, note the use of context manager using statement, important for access to "Unmanaged Resources", read a little about it.

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

NOTE: I don’t know where you declared the linked variable, but I put it with this.... you weren’t doing Open Connection. Your example, corrected:

        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {              
                SqlCommand query = 
                    new SqlCommand("INSERTO INTO gerarOS VALUES (@codOS,@nomeCliente, @modeloMoto, @quilometragem, @dataOS)", conexao);

                query.Parameters.AddWithValue("@codOS", codOS);
                query.Parameters.AddWithValue("@nomeCliente", nomeCliente);
                query.Parameters.AddWithValue("@modeloMoto", modeloMoto);
                query.Parameters.AddWithValue("@quilometragem", quilometragem);
                query.Parameters.AddWithValue("@dataOS", dataOS);
                query.Connection.Open();
                query.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //caso der erro na inserção
                MessageBox.Show(ex.Message);
                if (conexao.State == ConnectionState.Open) conexao.Close(); // fecha conexão
            } finally {
                if (conexao.State == ConnectionState.Open)
                    conexao.Close(); // fecha conexão
            }
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.