10

I have a stored procedure in sql which has a bit parameter.I need to pass the parameter value from c#.Can Someone help me how to do it. I passed true/false value to the bit parameter but it is not working.

CREATE PROCEDURE checkbit
      @id varchar(10),
@IsDeleted bit
AS
BEGIN

IF(@IsDeleted = 1)
    BEGIN
        DELETE FROM tablename WHERE ID = @id

        RETURN
    END
END

My C# code

Im using entity framework.Checking for bit value here.

bool chk;
if(val==1)
 chk=true;
else
 chk=false;
context.checkbit(id,chk)  
meda
  • 45,103
  • 14
  • 92
  • 122
iysh
  • 145
  • 1
  • 1
  • 9

2 Answers2

19

You can just use 0 or 1 for the BIT field

  • 1 for TRUE
  • 0 for FALSE

context.checkbit(id,val);// ? 1 : 0;
meda
  • 45,103
  • 14
  • 92
  • 122
  • I am using entity framework here.I used true/false but it is not working. – iysh Apr 21 '14 at 04:57
  • 6
    Did you read my answer? I said use **numbers** either **0** or **1** – meda Apr 21 '14 at 11:39
  • Why did SQL Server do this? I've been beating my head against a wall trying to find out why my bit value was not updating. I was setting the value to `true` and it executed with no errors, but the database kept showing `0`. –  Apr 21 '16 at 04:59
  • yes for SQL server there is no such things like true or false, only bit values – meda Apr 21 '16 at 05:00
  • there is now? something like 'TRUE' and 'False' for SQL Server. It converts those char to 0 or 1 for you. [here](https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15) – Allstar Jan 19 '22 at 12:00
3

IF your using Ado.net then try with below hope it will work.

SqlParameter param = new SqlParameter();
             param.ParameterName = "@Isstatus";
             param.Value = Isstatus;
             param.DbType = System.Data.DbType.Boolean
             cmd.Parameters.Add(param);

Entity Framework

if your sp return true or false then below you can use other wise you need to try with void.

using System.Data.Entity.Infrastructure;
using System.Data.Objects;

    public virtual bool Deletecustomer(int id ,bool IsDeleted )
    {
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<bool>("EXEC checkbit({0},{1})", id,IsDeleted ).SingleOrDefault();
    }
MSTdev
  • 4,507
  • 2
  • 23
  • 40