allanmark
asked on
Check value of a bit field in a dataset
Greetings
I am trying to check the value of a bit field and based on that, to update a label. I am getting an erro:
Error 1 Operator '==' cannot be applied to operands of type 'object' and 'bool' D:\MyStuff - Allan\Training\School_Keep er_ASP\Pag es_Admin\L ookup_Reli gion.aspx. cs 74 13 D:\...\School_Keeper_ASP\
My code:
if (dsReligions.Tables["Relig ions_List" ].Rows[lbx Religions. SelectedIn dex]["RL_S ystem"] == true)
Label4.Text = "System";
else
Label4.Text = "NON System";
My sql, that ceated the table:
CREATE TABLE [dbo].[Religions_List](
RL_No smallint IDENTITY(1,1) not null,
RL_Name varchar(50) not null,
RL_System bit not null default(0),
CONSTRAINT [PK_Religions_List] PRIMARY KEY.......
I am trying to check the value of a bit field and based on that, to update a label. I am getting an erro:
Error 1 Operator '==' cannot be applied to operands of type 'object' and 'bool' D:\MyStuff - Allan\Training\School_Keep
My code:
if (dsReligions.Tables["Relig
Label4.Text = "System";
else
Label4.Text = "NON System";
My sql, that ceated the table:
CREATE TABLE [dbo].[Religions_List](
RL_No smallint IDENTITY(1,1) not null,
RL_Name varchar(50) not null,
RL_System bit not null default(0),
CONSTRAINT [PK_Religions_List] PRIMARY KEY.......
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
int xyz = (int)dsReligions.Tables["R eligions_L ist"].Rows [lbxReligi ons.Select edIndex][" RL_System" ];
if ((int)dsReligions.Tables[" Religions_ List"].Row s[lbxRelig ions.Selec tedIndex][ "RL_System "] == 1)
Label4.Text = "System";
else
Label4.Text = "NON System";
ERROR: Specified cast is not valid.
if ((int)dsReligions.Tables["
Label4.Text = "System";
else
Label4.Text = "NON System";
ERROR: Specified cast is not valid.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it!!
Did as you suggested - the break point thingy ... value was TRUE. SO I played around and:
if ((BOOL)dsReligions.Tables[ "Religions _List"].Ro ws[lbxReli gions.Sele ctedIndex] ["RL_Syste m"] == true)
Label4.Text = "System";
else
Label4.Text = "NON System";
THANK YOU!!!
Did as you suggested - the break point thingy ... value was TRUE. SO I played around and:
if ((BOOL)dsReligions.Tables[
Label4.Text = "System";
else
Label4.Text = "NON System";
THANK YOU!!!
As a follow up note:
SQL Server data types do correspond to CLR data types:
bigint -> long
int -> int
smallint -> short
tinyint -> byte
bit -> bool
Be careful to always explicitly cast the result of a function (such as the column indexer of a datatable row, or the GetValue method of a data reader) to the CLR type you want before comparing to other values or assigning to variables.
e.g.
bool blnSystem = (bool)dsReligions.Tables[" Religions_ List"].Row s[lbxRelig ions.Selec tedIndex][ "RL_System "];
short shNo = (short)myReader.GetValue(0 ); // same as myReader.GetInt16(0)
All you have to be careful of are null database values - if you could have a NULL in a column, you can use a nullabe data type:
short? shNo = (short)myReader.GetValue(0 ); // same as myReader.GetInt16(0)
Andy
SQL Server data types do correspond to CLR data types:
bigint -> long
int -> int
smallint -> short
tinyint -> byte
bit -> bool
Be careful to always explicitly cast the result of a function (such as the column indexer of a datatable row, or the GetValue method of a data reader) to the CLR type you want before comparing to other values or assigning to variables.
e.g.
bool blnSystem = (bool)dsReligions.Tables["
short shNo = (short)myReader.GetValue(0
All you have to be careful of are null database values - if you could have a NULL in a column, you can use a nullabe data type:
short? shNo = (short)myReader.GetValue(0
Andy
0 means false