Stack Exchange
Check for existing entry in data table
private void txtBarcode_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                string connString = "Server=192.168.1.100;Database=product;Uid=newuser;Pwd=password";
                MySqlConnection conn = new MySqlConnection(connString);
                    //cmd = conn.CreateCommand();
                    //cmd.CommandText = "Select * From tblindividualproduct";
                    if (e.KeyCode == Keys.Enter)
                    {
                        if (txtBarcode.Text == "")
                        {
                            MessageBox.Show("Please Fill the correct ProductID");
                        }
                        else
                        {
                            if (HasRecord("tblindividualproduct.ProductID", connString) == false)
                            {
                                string sql = "Select Iproduct.ProductId, prodInfo.Name,Iproduct.UpdatedPrice From product.tblproductinformation AS prodInfo INNER JOIN product.tblindividualproduct AS Iproduct ON prodInfo.Code = Iproduct.Code where Iproduct.ProductID = @idText";
                                using (var adapt = new MySqlDataAdapter(sql, conn))
                                using (var cmd = new MySqlCommandBuilder(adapt)) //Not sure what you need this for unless you are going to update the database later.
                                {
                                    adapt.SelectCommand.Parameters.AddWithValue("@idText", txtBarcode.Text);
                                    adapt.Fill(dt);
                                    dgItems.ReadOnly = true;
                                    dgItems.DataSource = dt;
                                }
                            }
                        }
                    }
               }
            catch (MySqlException ex)
            {
                // output the error to see what's going on
                MessageBox.Show(ex.Message);
            }
        }

        static public bool HasRecord(string ProductID, string connString)
        {
            //add try catches where required
            bool foundRecord = true;
            Int32 numRecords = 0;
            string sql =
                "Select count(Iproduct.ProductId, prodInfo.Name,Iproduct.UpdatedPrice) From product.tblproductinformation AS prodInfo INNER JOIN product.tblindividualproduct AS Iproduct ON prodInfo.Code = Iproduct.Code where Iproduct.ProductID = @idText";
            using (MySqlConnection conn = new MySqlConnection(connString))
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.Add("@idText", SqlDbType.VarChar);
                cmd.Parameters["@idText"].Value = ProductID;
                try
                {
                    conn.Open();
                    numRecords = (Int32)cmd.ExecuteScalar();
                    if (numRecords == 0) foundRecord = false;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return foundRecord;
        }

I use this code to add items in my data set, can you help me on how to check for entry and wont accept it if it will have duplicate entry on the data set?
EDIT: I have updated my code to show you my current work.

What do you mean by duplicate entry ? You mean same rows in your query?

i mean if i enter a product id of 1 and i will enter it again. it wont accept it because it will have a duplicate entry in my data table.

The actual way to go about this is to make your ProductID as PrimaryKey in your database table tblindividualproduct and you can just handle the exception in code.
But if you dont have access to make changes to database schema, you can write a query to get that information
Select count(*) From tblindividualproduct where ProductID = @ProductIdText

A function to check if a row with that particular ID already exist, Insert only if this function returns false. connString will be your database connection string.
static public bool HasRecord(string ProductID, string connString)
{
    //add try catches where required
    bool foundRecord = true;
    Int32 numRecords = 0;
    string sql =
        "Select count(*) From tblindividualproduct where ProductID = @ProductIdText";
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@ProductIdText", SqlDbType.VarChar);
        cmd.Parameters["@ProductIdText"].Value = ProductID;
        try
        {
            conn.Open();
            numRecords = (Int32)cmd.ExecuteScalar();
            if(numRecords == 0) foundRecord = false;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    return foundRecord;
}

Then use it like in your function where your have your insert code
if(HasRecord("1","Data Source=192.x.x.x;Initial Catalog=mydbName;UserID=user;Password=passw;provider=SQLOLEDB") == false)
{
      //your insert code
}

http://www.dotnetperls.com/sqlclient
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx

i want to make a cashier system. i have different product ids. when i scan the barcode, i will delete it in my grid view so i want to locate if there is an entry the same in my data table..

thats what it checks to see if there an entry for that id.. did you read my answer ?

i got an error. Operator == cannot be applied to operands of type string and bool

that is the exact error in this line if(HasRecord("1","Data Source=192.x.x.x;Initial Catalog=mydbName;UserID=user;Password=passw;provider=SQLOLEDB" == false)

@MixAustria I have corrected it i missed a ).. if(HasRecord("1","Data Source=192.x.x.x;Initial Catalog=mydbName;UserID=user;Password=passw;provider=SQLOLEDB") == false)

the "1" in the condition is the table name i want to search?

its just an example.. the productid.. pls read the code to understand

im sorry. i mean product id. i got another error. cmd.Parameters.Add("@idText", SqlDbType.VarChar);
MySql.Data.MySqlClient.MySqlParameterCollection.Add(string,object) is obsolete: Add(string parameterName,Object value) has been deprecated i just only rename all my parameters to @idText

i have just updated my code. Try to take a peek. thanks
