-2

I try this code but in the where clause there's an error.

var subComTbl = from subCom in myDb.SubComTbls
where subCom.AuthorityID.ToString()== Authoritycombo.SelectedValue
select subCom;
SubComcombo.Visible = true;
SubComcombo.DataSource = subComTbl;
SubComcombo.DisplayMember = "SubComName";
SubComcombo.ValueMember = "SubComID";
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
MU Al-t
  • 9
  • 5

2 Answers2

1

It seems you should pay attention to compiler warnings.

Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'string'

The SelectedValue property is of type of object. You should use Authoritycombo.SelectedValue.ToString()

where subCom.AuthorityID.ToString()== Authoritycombo.SelectedValue.ToString()

Both operands of == should be of the same type, so if you use subCom.AuthorityID at left side, the other operand at right side should be of same type as AuthorityID or if you use Authoritycombo.SelectedValue.ToString() at left side the other operand at right side should be of type of string.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

You don't need convert SelectedValue and subCom.AuthorityID to string for comaring those values.

SelectedValue contains value of type same as type of Property(or Column) of DataSource

In your case, I assume AuthorityID and SubComID properties are Integers
So you need only check for null in SelectedValue before comparing

SubComcombo.Visible = true;
SubComcombo.DataSource = subComTbl;
SubComcombo.DisplayMember = "SubComName";
SubComcombo.ValueMember = "SubComID";

Int32 selectedID = -1; //default value which for sure not in the database
if(Authoritycombo.SelectedValue != null)
    selectedID = (Int32)Authoritycombo.SelectedValue;

var subComTbl = from subCom in myDb.SubComTbls
                where subCom.AuthorityID == selectedID 
                select subCom;
Fabio
  • 31,528
  • 4
  • 33
  • 72