Reputation: 2819
I want my LINQ query to count the number of instances a member is present in a table based on what the user enters into a textbox.
As a result, I have the following
protected void btnSubmit_Click(object sender, EventArgs e)
{
LoginDataContext lg = new LoginDataContext();
int logincheck = (from r in lg.tblOnlineReportingLogins
where r.MemberID == tbxMember.Text.ToString()
select r).Count();
When I debug this, the value appears as 0 when I know this is not the case.
Can someone point out where I am going wrong?
Upvotes: 1
Views: 3889
Reputation: 29164
I suppose the MemberId
property is an integer. So you cannot compare it against a string value. Text.ToString()
is redundant anyway, since Text
is already a string.
where r.MemberId.ToString() == tbxMember.Text
should work.
A cleaner solution would be to parse the Text
property into an int
value (together with some validity checking) and do the comparison using int
s, like
int targetId;
if (Int32.TryParse(tbxMember.Text, out targetId)) {
int logincheck = (from r in lg.tblOnlineReportingLogins
where r.MemberID == tbxMember.Text.ToString()
select r).Count();
// ...
} else {
MessageBox.ShowMessage("Invalid Id");
}
EDIT: Sorry, perhaps I was too quick, I assumed MemberId
was an integer. But in this case, your code shouldn't even have compiled. I'm just wondering about the upvotes :-) ...
Upvotes: 3