0

I want to allow the user to add a 'Category'.

However, before the Category is actually added, I want to make sure that it's not a duplicate.

Here is my code:

//ADD CATEGORY
private void addcat_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(addcattxt.Text))
    {
        MessageBox.Show("You must enter a valid category.", 
                        "Invalid Operation: Data Missing", 
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else 
    {
        foreach (DataRowView dvrow in catcombobox.Items)
        {
            if (dvrow.ToString() == addcattxt.Text)
            {
                MessageBox.Show("This category already exists.", 
                                "Invalid Operation: Duplicate Data", 
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                break;
            }

            var query = "INSERT INTO category_table (Category) VALUES(@cat);";
            using (var sqlcmd = new SqlCommand(query, sqlconnection))
            {
                sqlcmd.Parameters.AddWithValue("@cat", this.addcattxt.Text);
                sqlcmd.ExecuteNonQuery();
            }
            this.DialogResult = DialogResult.OK;
            this.Close();  
        }
    }
}

This code does not work, and it adds the Category entered by the user, regardless of whether its a duplicate or not.

I've also tried the following code for the foreach loop:

foreach (var item in catcombobox.Items), and still doesn't work.

How do I get it to work?

SOLUTION:

if (dvrow.Row["Category"].Equals(addcattxt.Text))
{
    MessageBox.Show("This category already exists.", 
                    "Invalid Operation: Duplicate Data", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

    break;
}
Jeff B
  • 8,572
  • 17
  • 61
  • 140
John
  • 447
  • 2
  • 8
  • 20
  • Why are you inserting to the database foreach dvrow that doesn't match? Perhaps you meant to put that outside the foreach? – ryanyuyu Apr 30 '15 at 20:58
  • @ryanyuyu yeah. I've made the changes, but still the same result. – John Apr 30 '15 at 21:02

3 Answers3

3

I think your code is not working because DataRowView.ToString() returns a representation of the object. What field of the row are you binding to? Your check should probably look more like:

if (dvRow.Row["Name"].Equals(addcattxt.Text)) ...
Jeff B
  • 8,572
  • 17
  • 61
  • 140
la-yumba
  • 81
  • 1
  • 4
  • Could you elaborate as to why `.Equals` is preferred for comparing strings? – Nathan Koop Apr 30 '15 at 21:13
  • @la-yumba Yes, I've tried using `.Equals` (it was my first choice). Your code suggestion worked. I initially didn't put the row name that it would refer to when performing the check. I've post the solution in my question. – John Apr 30 '15 at 21:16
  • I also question your justification for point 1; it may be valid for Java, but not C#. See [this answer](http://stackoverflow.com/a/1659107/1378739) – Setsu Apr 30 '15 at 21:41
  • 1
    Yes - correct, for String == does the same as .Equals; I removed that part of the answer, which was irrelevant – la-yumba Apr 30 '15 at 22:00
  • Thand, just making sure I wasn't missing anything – Nathan Koop Apr 30 '15 at 23:58
0

I think your code adds duplicates to the database because you’re not skipping the part to add it to the database if you hit a duplicate.

If it's not clear, try this in your code:

foreach (DataRowView dvrow in catcombobox.Items)
{
    if (dvrow.ToString() == addcattxt.Text)
    {
        MessageBox.Show("This category already exists.", 
                        "Invalid Operation: Duplicate Data", 
                        MessageBoxButtons.OK, MessageBoxIcon.Error);

        break;//<----- yeah jeff right no break here to
    }
    else
    {
        var query = "INSERT INTO category_table (Category) VALUES(@cat);";
        using (var sqlcmd = new SqlCommand(query, sqlconnection))
        {
            sqlcmd.Parameters.AddWithValue("@cat", this.addcattxt.Text);
            sqlcmd.ExecuteNonQuery();
        }
    }
}

I hope that resolves your probem! Good luck! :)

Kazimar
  • 375
  • 1
  • 3
  • 12
  • 1
    The break in the `if` statement should cause it to leave the `foreach` loop and never run the insert, even without the `else`. – Jeff B Apr 30 '15 at 22:38
0
if (!catcombobox.Items.Contains(addcattxt.Text.ToString()))
{
    ms_combobox.Items.Add(addcattxt.Text.ToString());
}
else
{
    MessageBox.Show("This category already exists.", 
                    "Invalid Operation: Duplicate Data", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
}

and this link for sample example :) http://www.mediafire.com/download/zb9gp1x1096it6s/Adding_to_Combobox_and_Checking_for_Duplicates.rar

Jeff B
  • 8,572
  • 17
  • 61
  • 140
Dark_M
  • 11
  • 2