如何检查文本框文本是否存在于从数据库中检索的数据列表中? [英] HOW to check textbox text is present in list of data which retrieved from database?

查看:126
本文介绍了如何检查文本框文本是否存在于从数据库中检索的数据列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查文本框文本是否存在于从数据库中检索的数据列表中。

(这与WEBAPI ENTITY FRAMEWORK HTTP REQ RES一起使用。)



我尝试过:



i want to check textbox text is present in list of data which is retrieved from database.
(THIS AM DOING WITH WEBAPI ENTITY FRAMEWORK HTTP REQ RES .)

What I have tried:

List<classname> h=response from db;
foreach(var item in h)
{
  //here if category name is ac and non ac and delux .
  if(item.categoryname==textbox1.text)
  {
    //code running must stop

    //BUT HERE PROBLEM IS ITS CHECKING ONLY FIRST INSERTED CATEGORY NAME LIKE AC..IF WE INSERT NONAC SECOND TIME ITS NOT CHECKING .HOW DO I CHECK LIST CATEGORY NAME COLUMN DATA COMPARING WITH TEXTBOX TEXT.
  }
}

推荐答案

如果您创建的列表已包含数据库包含的项目然后简单地通过foreach循环。根据我的理解,您在一个只运行一次或根本不运行多次的事件中创建了此代码。一种解决方案是在前面创建一个脚本,它将使用changed()方法,因此每次文本框文本更改时,此代码都会运行:

If the List you have created already has the items the DB contains then simply by the foreach loop. In my understanding you have created this code in an event that runs only one time or does not run as many times as needed. One solution would be to create a script in the front that will make use of the changed() method so every time the textbox text changes this code will run:
script language = javascript>
function txtChanged( TextBoxID)
{
   <%# ValidateExistance() %>
}
</script>





另一种方法是将此代码放入一个事件中每次控件状态发生变化时都会运行。一种方法是在aspx文件中继续设计模式,然后双击textBox。执行此操作后,将在后面的代码中创建一个事件,该事件可能会命名为:textboxText_Changed()

每次文本根据其名称更改时,此事件都将运行。将此代码放在该事件中。



Another way would be to put this code inside an event that it will be sure to run everytime the state of the control changes. One way to do that is to go on design mode in the aspx file and double click the textBox. Once you do that, an event will be created in the code behind that will be probably named: textboxText_Changed()
This event will run everytime the text changes according its name. Put this code inside of that event.

   foreach(var item in h){
      if(item.CategoryName.Contains(textbox1.text.ToString())
      {
         return false;
      }
      else
      {
         return true;
      }
   }
return false;





我希望这会有效......祝你好!



I hope this will work.... Best wishes!


down vote
read = com.ExecuteReader()

SqlDataReader has a function Read() that reads the next row from your query's results and returns a bool whether it found a next row to read or not. So you need to check that before you actually get the columns from your reader (which always just gets the current row that Read() got). Or preferably make a loop while(read.Read()) if your query returns multiple rows.


这篇关于如何检查文本框文本是否存在于从数据库中检索的数据列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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