Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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.
  }
}
Posted
Updated 3-Jul-17 2:38am
v2

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>


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.
C#
   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!
 
Share this answer
 
Comments
saimanisha 4-Jul-17 0:57am    
actually here iam not using queries ..am retrieving the data from db using webapi with entity framework..it retrieves total data and i need to check category name which iis there in data ac and non-ac and delux etc it has to check with textbox text ..

in the above case its checking only with first inserted item..it has to check with all items that is item.categoryname.with textbox text
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.
 
Share this answer
 
Comments
saimanisha 4-Jul-17 0:55am    
actually here iam not using queries ..am retrieving the data from db using webapi with entity framework..it retrieves total data and i need to check category name which iis there in data ac and non-ac and delux etc it has to check with textbox text ..

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