如果员工存在或不存在,我该如何检查 [英] How Do I Checked If Employee Exist Or Not

查看:99
本文介绍了如果员工存在或不存在,我该如何检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序,其中一个用户信息表单。我想检查员工是否存在用户用户滚动否也是PK。

这可能是通过使用文本更改或使用按钮单击事件

 asp:TextBox CssClass =form-controlrunat =serverMaxLength =7ID =txtRollNo/> ; 
< asp:RegularExpressionValidator 显示 = 动态 ValidationGroup = vgEmployee runat = server ID < span class =code-keyword> = revRollNo ControlToValidate = txtRollNo Requried = required ValidationExpression = ^ [A-Za -z0-9,/ \, - ] + $ ErrorMessage = 数字,Alpha, - 和/只 > < / asp:RegularExpressionValidator >
< asp:Button CssClass = btn btn-green btn-block ID = btn_Click 宽度 = 150 runat = server OnClick = btn_Click_Click CausesValidation = True 文字 = 检查员工 / >



和代码隐藏是

 受保护  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( 找不到记录 false );
}

}
}



问题是当我按下按钮总是返回记录发现,虽然没有针对该员工Id的记录。

解决方案

ErrorMessage = 数字,Alpha, - 和/只 > < / asp:RegularExpressionValidator >
< asp:按钮 CssClass = btn btn-green btn-block ID = btn_Click 宽度 = 150 runat = server OnClick = btn_Click_Click CausesValidation = True 文字 = < span class =code-keyword>检查员工 / >



和代码隐藏是

  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( 找不到记录 false );
}

}
}



问题是当我按下按钮总是返回记录发现,虽然没有针对该员工Id的记录。


你好,



而不是这个,



员工emp = _service.GetAllEmployee()。​​其中​​(x => x.Employee_Id == Employee.Id).LastOrDefault(); 





试试,



员工emp = _service.GetAllEmployee( )。哪里(x => x.Employee_Id == Employee.Id)。最后(); 





发生的事情是,如果没有找到记录,它返回默认记录。



希望这可以帮到你。



问候,

Prakriti Goyal


以断点模式签入你的代码来检查emp是否为null然后检查记录是否为空对象

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: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

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.

解决方案

" 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

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.


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


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


这篇关于如果员工存在或不存在,我该如何检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆