Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Help getting the number of records from a database


  • Please log in to reply
1 reply to this topic

#1 Braffin


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 15 November 2011 - 11:10 PM

I'm trying to pass a SELECT statement to my Access database and have it return the number of rows that match . I then have an If-Else statement, if the rows = 0 then do this, else do this. The problem is is that the If part is always being skipped, even if I put in a value that I know isn't in the database. I've included my code below and any help is greatly appreciated.

OleDbConnection connection = new OleDbConnection(con);

string inputquery = "SELECT COUNT(*) FROM vmtracker WHERE [Customer Name] = @name";
OleDbCommand query = new OleDbCommand(inputquery, connection);
query.Parameters.Add("@name", OleDbType.Char).Value = name.Text;
int dbresult = 0;
OleDbDataReader reader = query.ExecuteReader();
while (reader.Read())

if (dbresult == 0)
MessageBox.Show("Name already exists");
  • 0

#2 kemnet


    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 17 November 2011 - 06:13 AM

im in no way a professional and from just looking at the code not testing anything.

will dbresults ever be equal to zero if u incrementing it?in the while statement?
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download