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

string sql;
            OleDbCommand cmd;
            OleDbDataReader rdr;
           

            mDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" + Path.Combine(Application.StartupPath, "..\\..\\Company.mdb");
            mDB.Open();
            sql = "Select COUNT (*) ProductID FROM Product";



            cmd = new OleDbCommand(sql, mDB);

            rdr = cmd.ExecuteReader();
            if (txtProductID.Text == (string)rdr["ProductID"] )
            {

                MessageBox.Show("ID exixst");

            }
            else
            {
                return;

            }

Hi!

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

string sqlString = "Select ProductID FROM Product where ProductID="+txtProductID.Text;
OleDbCommand dbCmd = new OleDbCommand(sqlString, mDB);
mDB.Open();
OleDbDataReader reader = dbCmd.ExecuteReader();
if (reader.Read())
{
	MessageBox.Show("ID exixst");
}
else
{
  // do something else here
}

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


Daniel

Hi!

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

string sqlString = "Select ProductID FROM Product where ProductID="+txtProductID.Text;
OleDbCommand dbCmd = new OleDbCommand(sqlString, mDB);
mDB.Open();
OleDbDataReader reader = dbCmd.ExecuteReader();
if (reader.Read())
{
	MessageBox.Show("ID exixst");
}
else
{
  // do something else here
}

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


Daniel

ohh! great! Daniel green. thx!
Is easy code but serves

@ DanielGreen,

I go with your code too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.