How to pass correct object value in SqlParameter for datatype bit?

10,354

Solution 1

bit refers to Boolean

so you would pass a boolean value in parameter's value

Ex :

CmdGetDetails.Parameters.AddWithValue("isExpired", chkExpired.Checked); 

There is no addtional need to use a if block.

Solution 2

            param.ParameterName = "@isExpired";
            param.Value =chkExpired.Checked; 
            param.DbType = System.Data.DbType.Boolean;
            cmd.Parameters.Add(param);

Solution 3

Just use the value of your checkbox (chkExpired.Checked):

CmdGetDetails.Parameters.Add(new SqlParameter("@isExpired", chkExpired.Checked));
Share:
10,354
JoJo
Author by

JoJo

I was born.. ..later I fiddled with programming on a Commadore Vic 20.. then a Trash-80 (TSR-80) at the school during lunch break... played with a little bit of basic... my Dad had an 8086 followed by a 286... I needed to build one of these things for my own!!! I finally did build my first computer and it was either a 386 or 486 I can't recall now. Been too long. SO yeah eventually I was putting up BBS boards etc until one day, the Internet, in its HTML incarnation arrived and with it the return of my joy of programming.

Updated on June 29, 2022

Comments

  • JoJo
    JoJo almost 2 years

    I am trying to pass a bit value only if needed (is checked).

    How do I do this correctly? I am not getting a change in my dataset. SQL Server 2008.

    if (chkExpired.Checked)
        CmdGetDetails.Parameters.Add(new SqlParameter("@isExpired", 1));
    
    • Patrick Hofman
      Patrick Hofman about 10 years
      You should include more info. What is your database platform? Where is the rest of your query?
    • Habib
      Habib about 10 years
      isExpired looked like boolean field, represented as bit in SQL Server. Are you sure you have an int field
    • JoJo
      JoJo about 10 years
      I need to cast that 1 int value dont I?
  • sam
    sam over 5 years
    that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value
  • sam
    sam over 5 years
    that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value
  • sam
    sam over 5 years
    that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value