How to check if an ID exist in database

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 1
Reputation: sara455 is an unknown quantity at this point 
Solved Threads: 0
sara455 sara455 is offline Offline
Newbie Poster

How to check if an ID exist in database

 
0
  #1
Oct 22nd, 2009
Im doing an application n c# and using Ms Access 2003. i want to check whether a product id already exist in the database. Below is part of my code
  1. string sql;
  2. OleDbCommand cmd;
  3. OleDbDataReader rdr;
  4.  
  5.  
  6. mDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" + Path.Combine(Application.StartupPath, "..\\..\\Company.mdb");
  7. mDB.Open();
  8. sql = "Select COUNT (*) ProductID FROM Product";
  9.  
  10.  
  11.  
  12. cmd = new OleDbCommand(sql, mDB);
  13.  
  14. rdr = cmd.ExecuteReader();
  15. if (txtProductID.Text == (string)rdr["ProductID"] )
  16. {
  17.  
  18. MessageBox.Show("ID exixst");
  19.  
  20. }
  21. else
  22. {
  23. return;
  24.  
  25. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: DanielGreen is an unknown quantity at this point 
Solved Threads: 2
DanielGreen DanielGreen is offline Offline
Newbie Poster
 
0
  #2
Oct 22nd, 2009
Hi!

One possibility is, that you do an other sql-statement where you try to select the desired ProduktID:

  1. string sqlString = "Select ProductID FROM Product where ProductID="+txtProductID.Text;
  2. OleDbCommand dbCmd = new OleDbCommand(sqlString, mDB);
  3. mDB.Open();
  4. OleDbDataReader reader = dbCmd.ExecuteReader();
  5. if (reader.Read())
  6. {
  7. MessageBox.Show("ID exixst");
  8. }
  9. else
  10. {
  11. // do something else here
  12. }

I have not tested the code, but i think this should work!


Daniel
Reply With Quote Quick reply to this message  
Reply

Tags
c#, check, database

Message:




Views: 1223 | Replies: 1
Thread Tools Search this Thread



Tag cloud for c#, check, database
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC