944,116 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4213
  • C# RSS
Oct 22nd, 2009
0

How to check if an ID exist in database

Expand Post »
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
C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sara455 is offline Offline
1 posts
since Oct 2009
Oct 22nd, 2009
0
Re: How to check if an ID exist in database
Hi!

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

C# Syntax (Toggle Plain Text)
  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
Reputation Points: 16
Solved Threads: 4
Light Poster
DanielGreen is offline Offline
28 posts
since Oct 2009
Oct 12th, 2011
0
Re: How to check if an ID exist in database
Hi!

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

C# Syntax (Toggle Plain Text)
  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


ohh! great! Daniel green. thx!
Is easy code but serves
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hacoga is offline Offline
1 posts
since Oct 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: What happens if you inherit multiple interfaces and they have conflicting method name
Next Thread in C# Forum Timeline: Click event on a 3D object





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC