Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a web application,in which a user information form.i want to check if employee exist or not when user a user roll no which is also PK.
THAT may be by using on text change or using button click event
ASP.NET
asp:TextBox CssClass="form-control" runat="server" MaxLength="7" ID="txtRollNo"    />
                    <asp:RegularExpressionValidator Display="Dynamic" ValidationGroup="vgEmployee" runat="server" ID="revRollNo" ControlToValidate="txtRollNo" Requried="required" ValidationExpression="^[A-Za-z0-9,/\,-]+$" ErrorMessage="Number,Alpha,- and / only" ></asp:RegularExpressionValidator>
                     <asp:Button CssClass="btn btn-green btn-block " ID="btn_Click"  Width="150" runat="server" OnClick="btn_Click_Click" CausesValidation="True" Text="Check Employee"/>

and codebehind is
C#
protected void btn_Click_Click(object sender, EventArgs e)
       {
           if (!string.IsNullOrEmpty(txtRollNo.Text))
           {
               Employee emp = _service.GetAllEmployee().Where(x => x.Employee_Id==Employee.Cnic).LastOrDefault();

               if (emp != null)
               {
                   MsgLitteral.ShowNotificationInline("Record Found", false);

               }
               else
               {
                   MsgLitteral.ShowNotificationInline("Record Not Found", false);
               }

           }
       }

Problem is that when i press the button is always return Record found,although there is no record against that employee Id.
Posted
Comments
[no name] 27-Oct-14 5:59am    
Arent you gonna use txtRollNo.Text in the filter criteria ?
Sajid227 27-Oct-14 6:01am    
on any way that might be good
Sergey Alexandrovich Kryukov 27-Oct-14 6:01am    
What is Employee.Cnic, how is it calculate?
This is related to the question by Praneet Nadkar above.
Did you use the debugger to find the problem?
—SA
Sajid227 27-Oct-14 6:13am    
there is no error display by debugger
Sergey Alexandrovich Kryukov 27-Oct-14 6:16am    
Who told you that the debugger should display any "error"? (You probably meant "exception"...)
Do I have to explain what the debugger does? Use the debugger. As you are really confused about the use of the debugger, please learn the basics of what it can do. Basically, you can execute step by step and examine some variables and members, and even expressions, as you go.
—SA

Hello,

Instead of this,

Employee emp = _service.GetAllEmployee().Where(x => x.Employee_Id==Employee.Id).LastOrDefault();


try,

Employee emp = _service.GetAllEmployee().Where(x => x.Employee_Id==Employee.Id).Last();


What is happening is that, it is returning default record in case if no record is found.

Hope this helps you.

Regards,
Prakriti Goyal
 
Share this answer
 
Comments
Sajid227 27-Oct-14 6:28am    
same result,return record found in both cases.
Prakriti Goyal 27-Oct-14 6:30am    
Employee emp = _service.GetAllEmployee().Where(x => x.Employee_Id==Employee.Id).Last();

Debug and check what value emp is returning in this code.
Sajid227 27-Oct-14 6:36am    
Employee emp = _service.GetAllEmployee().Where(x => x.Employee_Id==Employee.Id).Last();
that return all field value null except for dates for both cases,if record is database or not
Sajid227 27-Oct-14 6:37am    
is there another solution to check the record using linq
Prakriti Goyal 27-Oct-14 7:23am    
Employee emp = new Employee();
emp = _service.GetAllEmployee().Where(x => x.Employee_Id==txtRollNo.Text).Last();

Try this
to check in ur code in break point mode to check in emp is null or not then check record is null or not in ur object
 
Share this answer
 
Hello,
seeing your code No Camparsion is being Made with the Id that has been entered into the textbox.so make use of that Id for Camparsion.
C#
Employee emp = _service.GetAllEmployee().Where(x =>x.Employee_Id==txtRollNo.Text).Last();

then check whether it is null or not.
Hope It Helps you.
Try To Surround your code with try Catch .
 
Share this answer
 
Comments
Sajid227 27-Oct-14 16:51pm    
protected void btn_Click_Click(object sender, EventArgs e)
{
try
{

if (!string.IsNullOrEmpty(txtRollNo.Text))
{
Employee emp = _service.GetAllEmployee().Where(x => x.Employee_Id == txtRollNo.Text).Last();

if (emp != null)
{
Console.WriteLine("Record Found");
}
else
{
Console.WriteLine("Record Not Found");
}


}
}
catch (Exception ex) { HandException(ex); }
after trying this code an excetion is occur that state Sequence contain no element
Hello

Employee emp = _service.GetAllEmployee().Where(x=>x.Employee_Id==txtRollNo.Text).LastOrDefault();

if(emp!=null)
{
Console.WriteLine("Record Found");
}
else
{
Console.WriteLine("Record Not Found");
}
 
Share this answer
 
Comments
Sajid227 27-Oct-14 16:42pm    
there is no display of given result,in both cases.after debugging it return null in both condition
C#
protected void btn_Click_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtRollNo.Text))
            {
                var employee = _service.GetEmployee(txtRollNo.Text);
                if (employee != null)
                
                {
                    MsgLitteral.ShowNotificationInline("Record Found",true);
                }
                else
                {
                    MsgLitteral.ShowNotificationInline("Record not Found", false);
                }
            }

i just change the above code and that fine,thanx to all
 
Share this answer
 

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