Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this must sound really simple to some and I apologise for that but I am new to C# and LINQ.

I am trying to write a method which searches whether an ID which is represented as an INT in my database exists. I have written a method to handle errors and the purpose of the code I have written is to stop the insert statement if the unique ID is present, instead of trying to insert it and failing. The error handing works but is not working for this one function which is very frustrating.

I have done some research on the Internet and have tried several methods to solve my problem by so far nothing has worked. I wrote two methods which I believe to be alright but it is still not doing what I want it to do. The methods I have tried are below. If anyone can put me on the right track I would be very grateful!

C#
int EmployeeIDCatched = int.Parse(EmployeeIDTextBox.Text);

var Database = new DataSet();

// The first method I tried

bool Query1 = Database.Employee.Any(Employee => Employee.EmployeeID == EmployeeIDCatched);

if (Query1 == true)
{
ErrorList.Add("The unique ID is taken");
Valid = false;
}

// The second method I tried

var Query2 = from Employee in Database.Employee
             where (Employee.EmployeeID == EmployeeIDCatched)
             select Employee.EmployeeID;

if (Query2.Count() > 0)
{
ErrorList.Add("The unique ID is taken");
Valid = false;
}


I have called my Dataset Database. I have a table called Employee and am searching a field inside the table which is called EmployeeID. Both the value I am pulling from the text box and searching for in the database are integers.
Posted
Comments
Shahin Khorshidnia 10-Mar-12 7:36am    
Please explain more. It seems your code are ok, so What do you exactly need?
your code (the first one) is adding a error message to your error List, if there is the same ID in database. So, what's the problem? What do you want it to do?

Try this:

C#
int Query2 = (from Employee in Database.Employee
             where Employee.EmployeeID == EmployeeIDCatched
             select (Employee.EmployeeID)).Count();

if (Query2 > 0)
{
ErrorList.Add("The unique ID is taken");
Valid = false;
}


Hopefully will work.
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 10-Mar-12 7:29am    
That maybe so, but it has nothing more than IanMac91's code. Ofcourse I didn't vote this.
Try this:



int Query2 = (from Employee in Database.Employee
             where Employee.EmployeeID == EmployeeIDCatched
             select new{Employee.EmployeeID}).Count();

if (Query2 > 0)
{
ErrorList.Add("The unique ID is taken");
Valid = false;
}
 
Share this answer
 
Comments
IanMac91 10-Mar-12 7:00am    
Thanks for your reply. I keep getting the same error message as before however this time I am catching the value returned from the query in a message box and the value is 0 even when I am inputting a value which I know to be in the database. I keep getting the following error message:
"Column 'EmployeeID' is constrained to be unique. Value '123' is already present."
Shahin Khorshidnia 10-Mar-12 8:12am    
I think you are inputing an ID twice. I mean, you checked 123 then Query2 =0 and it's ok to insert. You insert it successfully (No exception)and ...you try to insert it again ("Column 'EmployeeID' is constrained to be unique. Value '123' is already present.")
Trace your program and find the reason.
André Kraak 10-Mar-12 7:01am    
This is a repost of Solution 1.
IanMac91 10-Mar-12 7:09am    
They look fairly similar but this is slightly different from the first post. The brackets are different and he has added a "new" statement and some curly brackets. I am getting similar results from statements.
sravani.v 10-Mar-12 7:06am    
This is not a repost.
you please see once agian Mr.Andre

Added "new" key word in my solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900