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

4

6

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));
Kettledrum answered 2/5, 2014 at 13:14 Comment(3)
You should include more info. What is your database platform? Where is the rest of your query?Flaherty
isExpired looked like boolean field, represented as bit in SQL Server. Are you sure you have an int fieldBlocking
I need to cast that 1 int value dont I?Kettledrum
B
13

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.

Bacchanal answered 2/5, 2014 at 13:18 Comment(1)
that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen valueEvulsion
H
3
            param.ParameterName = "@isExpired";
            param.Value =chkExpired.Checked; 
            param.DbType = System.Data.DbType.Boolean;
            cmd.Parameters.Add(param);
Hillery answered 2/5, 2014 at 13:18 Comment(1)
that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen valueEvulsion
F
1

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

CmdGetDetails.Parameters.Add(new SqlParameter("@isExpired", chkExpired.Checked));
Flaherty answered 2/5, 2014 at 13:19 Comment(0)
O
0

you'll want to add the parameter either way, as a boolean :

CmdGetDetails.Parameters.Add(new SqlParameter("@isExpired", chkExpired.Checked));
Oca answered 2/5, 2014 at 13:19 Comment(1)
that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen valueEvulsion

© 2022 - 2024 — McMap. All rights reserved.