Click here to Skip to main content
15,171,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI:
I'm using LINQ to sql with c#
I have a text box on a web page , after entering a value to that box
I need to validate whither if this value exist in myTable or not
C#
Var xmyTable = (from ymyTable in objDataContext.myTables
                              Select new 
                             {
                               ymyTable.myCode
                              ymyTable.myNmae
                             }

please help
Posted
Updated 1-Mar-21 1:40am

Hi you can write query like this using lambda expresion

http://stackoverflow.com/questions/6330682/select-count-in-linq-to-sql-c-sharp[^]
C#
if(objDataContext.myTables.Count((a)=>a.name=="some name")==0)
{
//Record not present
}
else
{
//Record is present
}


C#
(a)=>a.name=="some name"
is for where condition
hope it is helpful to you.
   
C#
var v=objDataContext.myTables.FirstOrDefault(p=>p.myCode=text box value);
if (!v=Null)
{
}


Hope it helps
   
1st of all store the textbox's value in a string and retrieve value from database matching that sting. here i am taking name as the textbox's value.


C#
 string s = textBox1.text;
var xmyTable = (from ymyTable in objDataContext.myTables where ymyTable.myName == s
                  Select new {ymyTable.myCode,ymyTable.myNmae}).FirstorDefault();
if(xmyTable.Count>0)
{
//your logic for record found.
} 
else
{
//your logic for record not found
}  
   
string s = textBox1.text.Trim();
if (!objDataContext.table1.Any(a => a.col1 == s))
{//put insert statement
}
   

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