Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to control the database searching?
if we make a search process through the user id stored in the database, so we search for a specific id, i need to control the if statement like that

if ( id exist)
{ do some thing}
else //not exist
{do some thing}


i am using c#
please write in code

database is access
Posted
Updated 17-Jul-11 9:56am
v2

try this:

C#
SqlCommand cmd = new SqlCommand("SELECT name FROM myTable WHERE id=1", con);
string resultedname = (string)cmd.ExecuteScalar();
if (resultedname!="")
   {
   // id Exists
   }
else
{
 // id does not Exists
}


hope it helps :)
for further queries comment here!!
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 17-Jul-11 15:42pm    
ExecuteScaler? A typo I suspect.
Uday P.Singh 17-Jul-11 15:46pm    
yes Manfred thanks for correcting me , i updated the answer
Manfred Rudolf Bihy 17-Jul-11 18:08pm    
My 5!
Uday P.Singh 18-Jul-11 1:12am    
thanks Manfred :)
Espen Harlinn 17-Jul-11 18:32pm    
Nice and simple, my 5
Try:
SqlCommand cmd = new SqlCommand("SELECT * FROM myTable WHERE id=1234", con);
SqlDataReader r = cmd.ExecuteReader();
if (r.Read())
   {
   // id Exists
   }
 
Share this answer
 
Comments
Espen Harlinn 17-Jul-11 18:32pm    
Nice and simple, my 5

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