Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] this is a really simple question

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Resolved [RESOLVED] [2.0] this is a really simple question

    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

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [2.0] this is a really simple question

    c# Code:
    1. //your SqlConnection here, name it conn
    2.  
    3. //SqlCommand
    4. Sqlcommand cmd = new SqlCommand();
    5. cmd.CommandText = "Select Count(studyIdNumber) from table_name WHERE studyIdNumber='FF12033'";
    6.  
    7. cmd.Connection = conn;
    8.  
    9. conn.Open();
    10.  
    11. int num_of_instance = cmd.ExecuteScalar();
    12.  
    13. conn.Close();
    14.  
    15. if (num_of_instance == 1)
    16. {
    17.      //Item already inserted
    18. }
    19. else
    20. {
    21.      //Insert new Item
    22. }
    Hope it helps!!
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: [2.0] this is a really simple question

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width