Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass Bool (BIT) parameter to SQL server?

Tags:

c#

sql

asp.net

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)  
like image 761
iysh Avatar asked Apr 21 '14 03:04

iysh


2 Answers

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();
    }
like image 129
MSTdev Avatar answered Oct 20 '22 15:10

MSTdev


You can just use 0 or 1 for the BIT field

  • 1 for TRUE
  • 0 for FALSE

context.checkbit(id,val);// ? 1 : 0;
like image 22
meda Avatar answered Oct 20 '22 16:10

meda