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));
3
  • You should include more info. What is your database platform? Where is the rest of your query? Commented May 2, 2014 at 13:16
  • 1
    isExpired looked like boolean field, represented as bit in SQL Server. Are you sure you have an int field Commented May 2, 2014 at 13:17
  • I need to cast that 1 int value dont I? Commented May 2, 2014 at 13:18

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value
3
            param.ParameterName = "@isExpired";
            param.Value =chkExpired.Checked; 
            param.DbType = System.Data.DbType.Boolean;
            cmd.Parameters.Add(param);

1 Comment

that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value
1

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

CmdGetDetails.Parameters.Add(new SqlParameter("@isExpired", chkExpired.Checked));

Comments

0

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

CmdGetDetails.Parameters.Add(new SqlParameter("@isExpired", chkExpired.Checked));

1 Comment

that way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.