PDA

Click to See Complete Forum and Search --> : [RESOLVED] [2.0] this is a really simple question


r0k3t
Jul 5th, 2007, 09:28 AM
And I should know it but I don't...

What is the simplest way to check for the existence of a value in a database. Like I wanna know if studyIdNumber FF12033 already has been inserted. I have like 14 lines of code to do it. There has to be a better way.

Thanks

Harsh Gupta
Jul 5th, 2007, 10:15 AM
//your SqlConnection here, name it conn

//SqlCommand
Sqlcommand cmd = new SqlCommand();
cmd.CommandText = "Select Count(studyIdNumber) from table_name WHERE studyIdNumber='FF12033'";

cmd.Connection = conn;

conn.Open();

int num_of_instance = cmd.ExecuteScalar();

conn.Close();

if (num_of_instance == 1)
{
//Item already inserted
}
else
{
//Insert new Item
}
Hope it helps!!

r0k3t
Jul 5th, 2007, 11:14 AM
Wowser... As soon as you posted that I realized I had done that before. what can I say? It is the day after the fourth.

Thanks!