How to pass correct object value in SqlParameter for datatype bit?
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));
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, 2022Comments
-
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 about 10 yearsYou should include more info. What is your database platform? Where is the rest of your query?
-
Habib about 10 years
isExpired
looked like boolean field, represented asbit
in SQL Server. Are you sure you have anint
field -
JoJo about 10 yearsI need to cast that 1 int value dont I?
-
-
sam over 5 yearsthat way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value
-
sam over 5 yearsthat way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value
-
sam over 5 yearsthat way will return only checked value not both checked or unchecked... what I really need is to pass the chosen value