Guru G
Guru G

Reputation: 97

can not implicitly convert 'int' to 'bool'

public bool InsertBank()
{
    try
    {
        SqlCommand cmd = new SqlCommand("INSERTBANKMASTER", dal.con);
        cmd.Parameters.Add("@Bank_Name",SqlDbType.NVarChar).Value = Bank_Name;
        cmd.Parameters.Add("@Bank_ShortName",SqlDbType.NVarChar).Value = Bank_ShortName;
        cmd.Parameters.Add("@CreditCard_Commission_Percent",SqlDbType.Float).Value = CreditCard_Commission_Percent;
        cmd.CommandType = CommandType.StoredProcedure;
        return cmd.ExecuteNonQuery();                
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

i cannot resolve this error.. im new in c#.. so any body let me know how to correct it.

Upvotes: 1

Views: 2761

Answers (5)

Chamika Sandamal
Chamika Sandamal

Reputation: 24302

use following code.

return Convert.ToBoolean(cmd.ExecuteNonQuery());

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062660

At a guess:

return cmd.ExecuteNonQuery() != 0;

But it depends on your query. Since this is a SPROC you might need to add a parameter with direction ReturnValur. Also note: your try/catch does nothing except destroy the .StackTrace and you should be "using" the SqlCommand.

Upvotes: 2

Haris Hasan
Haris Hasan

Reputation: 30097

Problem is that your method InsertBank() declaration is not consistent with the value you are returning inside the method. Your method declaration says that it expect a bool True or False in return value but you are returning an Integer as cmd.ExecuteNonQuery(); returns int not bool

You should think what you actually want in return from your method.

Upvotes: 0

Guffa
Guffa

Reputation: 700262

The ExecuteNonQuery method returns an int, which you try to use as return value for the method.

If you want to check if at least one record was affected by the query:

return cmd.ExecuteNonQuery() >= 1;

If you want to check if exactly one record was affected by the query:

return cmd.ExecuteNonQuery() == 1;

Upvotes: 1

cudahead
cudahead

Reputation: 153

Your method is supposed to return a value of type bool. But you return a value of type int, because cmd.ExecuteNonQuery() returns the number of affected rows of your query as an int.

Upvotes: 2

Related Questions