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 = B开发者_运维知识库ank_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.
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
.
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.
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;
use following code.
return Convert.ToBoolean(cmd.ExecuteNonQuery());
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.
精彩评论