Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5996413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:05:59+00:00 2026-05-23T00:05:59+00:00

I am trying to see if an ID exists in a database in this

  • 0

I am trying to see if an ID exists in a database in this case it’s vetID.

To see if the result of the SQL query is successfull I am trying to use rdata.FieldCount

FieldCount always seems to return 11 even if the input ID (variable i) is = -400 (an id number that cannot exist in the database). I have also tried with other numbers that are not possible such as 100 or 800 (the database has only 1 – 5 items in it)

var mySQLCommand = new SqlCeCommand("SELECT * FROM vets WHERE vetID = @ID", dbCon);
mySQLCommand.Parameters.AddWithValue("ID", i);
SqlCeDataReader rdata = mySQLCommand.ExecuteReader();
if (rdata.FieldCount >= 1) 
{
    MessageBox.Show(" HAS ROWS "); go = true; 
}
else 
{ 
    MessageBox.Show("NO ROWS RETURNED"); 
}
MessageBox.Show("rdata FieldCount " + rdata.FieldCount + " i has value " + i);

Is there a more elegant way to check an ID is still valid in the database and has not been deleted?

OK.. before you call me Stupid … im guessing the FieldCount is just the exactly that the FieldCount and not the FieldCount of the returned Query … doh…

But the question still stands is there an elegant way to know if the ID is still there in the database?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:05:59+00:00Added an answer on May 23, 2026 at 12:05 am

    If you are using a datareader you can’t get a rowcount using fieldcount. But if you wanted to leave the query the same you’d do:

    var mySQLCommand = new SqlCeCommand("SELECT * FROM vets WHERE vetID = @ID", dbCon);
    mySQLCommand.Parameters.AddWithValue("ID", i);
    SqlCeDataReader rdata = mySQLCommand.ExecuteReader();
    if (rdata.read() ) 
    {
        MessageBox.Show(" HAS ROWS "); 
    go = true; 
    }
    else 
    { 
        MessageBox.Show("NO ROWS RETURNED"); 
    }
    MessageBox.Show("rdata FieldCount " + rdata.FieldCount + " i has value " + i);
    

    Another way to check is to use a select count(*) to see if it’s in the table and that would be 0 if it doesn’t exist.

    var mySQLCommand = new SqlCeCommand("SELECT count(*) FROM vets WHERE vetID = @ID", dbCon);
                mySQLCommand.Parameters.AddWithValue("ID", i);
                SqlCeDataReader rdata = mySQLCommand.ExecuteReader();
    int rowCount = 0;
    if ( rdata.read() ) {
        rowCount = rdata[0];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to query my database based on a specific id. String sql
Trying to write a function to see how often an object exists and give
I am trying to see from an SQL console what is inside an Oracle
SEE: Update timestamp column in Application or Database? I'm trying to model something similar
I'm trying to use sqlite as database for a symbian app but I can't
I am trying to setup a mysql database, but I am getting this error
I'm trying to use an in-memory SQLite database to test my data layer which
I am trying to check if certain ID already exists in database. When it
I am trying to add user-defined aggregates to a SQL Server 2008 database whose
I'm trying to go inside of my database and see if a certain submission

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.