Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加到组合框并检查重复项(C#)_C#_Winforms_Combobox - Fatal编程技术网

添加到组合框并检查重复项(C#)

添加到组合框并检查重复项(C#),c#,winforms,combobox,C#,Winforms,Combobox,我想允许用户添加“类别” 然而,在实际添加类别之前,我想确保它不是重复的 这是我的密码: //ADD CATEGORY private void addcat_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(addcattxt.Text)) { MessageBox.Show("You must enter a valid category.",

我想允许用户添加“类别”

然而,在实际添加类别之前,我想确保它不是重复的

这是我的密码:

//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();  
        }
    }
}
此代码不起作用,它会添加用户输入的类别,无论其是否重复

我还为
foreach
循环尝试了以下代码:

foreach(catcombobox.Items中的var项)
,但仍然不起作用

我如何让它工作

解决方案:

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

    break;
}

我认为您的代码不起作用,因为
DataRowView.ToString()
返回对象的表示形式。您要绑定到行的哪个字段?您的支票可能更像:

if (dvRow.Row["Name"].Equals(addcattxt.Text)) ...

我认为您的代码不起作用,因为
DataRowView.ToString()
返回对象的表示形式。您要绑定到行的哪个字段?您的支票可能更像:

if (dvRow.Row["Name"].Equals(addcattxt.Text)) ...

我认为您的代码不起作用,因为
DataRowView.ToString()
返回对象的表示形式。您要绑定到行的哪个字段?您的支票可能更像:

if (dvRow.Row["Name"].Equals(addcattxt.Text)) ...

我认为您的代码不起作用,因为
DataRowView.ToString()
返回对象的表示形式。您要绑定到行的哪个字段?您的支票可能更像:

if (dvRow.Row["Name"].Equals(addcattxt.Text)) ...

我认为您的代码会将重复项添加到数据库中,因为如果遇到重复项,您不会跳过将其添加到数据库中的部分

如果不清楚,请在代码中尝试以下操作:

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();
        }
    }
}
foreach(catcombobox.Items中的DataRowView-dvrow)
{
if(dvrow.ToString()==addcatText.Text)
{
MessageBox.Show(“此类别已存在。”,
“无效操作:重复数据”,
MessageBoxButtons.OK,MessageBoxIcon.Error);

break;//我认为您的代码会将重复项添加到数据库中,因为如果遇到重复项,您不会跳过将其添加到数据库中的部分

如果不清楚,请在代码中尝试以下操作:

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();
        }
    }
}
foreach(catcombobox.Items中的DataRowView-dvrow)
{
if(dvrow.ToString()==addcatText.Text)
{
MessageBox.Show(“此类别已存在。”,
“无效操作:重复数据”,
MessageBoxButtons.OK,MessageBoxIcon.Error);

break;//我认为您的代码会将重复项添加到数据库中,因为如果遇到重复项,您不会跳过将其添加到数据库中的部分

如果不清楚,请在代码中尝试以下操作:

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();
        }
    }
}
foreach(catcombobox.Items中的DataRowView-dvrow)
{
if(dvrow.ToString()==addcatText.Text)
{
MessageBox.Show(“此类别已存在。”,
“无效操作:重复数据”,
MessageBoxButtons.OK,MessageBoxIcon.Error);

break;//我认为您的代码会将重复项添加到数据库中,因为如果遇到重复项,您不会跳过将其添加到数据库中的部分

如果不清楚,请在代码中尝试以下操作:

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();
        }
    }
}
foreach(catcombobox.Items中的DataRowView-dvrow)
{
if(dvrow.ToString()==addcatText.Text)
{
MessageBox.Show(“此类别已存在。”,
“无效操作:重复数据”,
MessageBoxButtons.OK,MessageBoxIcon.Error);
中断//
此链接用于示例:)

此链接用于示例:)

此链接用于示例:)



示例链接:)

为什么要插入不匹配的数据库foreach dvrow?也许你想把它放在foreach之外?@ryanyyu是的。我做了更改,但结果仍然相同。为什么要插入不匹配的数据库foreach dvrow?也许你想把它放在foreach之外foreach?@ryanyyu是的。我做了更改,但结果仍然相同。为什么要插入不匹配的数据库foreach dvrow?也许你想将其放在foreach之外?@ryanyyu是的。我做了更改,但结果仍然相同。为什么要插入不匹配的数据库foreach dvrow?也许你是说是的。我做了一些改变,但结果仍然是一样的。你能解释一下为什么比较字符串时首选
.Equals
吗?@la yumba是的,我试过使用
.Equals
(这是我的第一选择)。您的代码建议有效。我最初没有输入它在执行检查时将引用的行名称。我在问题中发布了解决方案。我还质疑您对第1点的理由;它可能对Java有效,但对C#无效。请参阅Yes-correct,因为String==与.Equals相同;我删除了答案中的那部分,即IRELevanthand,只是确保我没有遗漏任何东西。你能详细说明为什么比较字符串时首选
.Equals
吗?@la yumba是的,我试过使用
.Equals
(这是我的第一选择)。您的代码建议有效。我最初没有输入它在执行检查时将引用的行名称。我在问题中发布了解决方案。我还质疑您对第1点的理由;它可能对Java有效,但对C#无效。请参阅Yes-correct,因为String==与.Equals相同;我删除了答案中的那部分,即IRELevanthand,只是确保我没有遗漏任何东西。你能详细说明为什么比较字符串时首选
.Equals
吗?@la yumba是的,我试过使用
.Equals
(这是我的第一选择)。您的代码建议有效。我最初没有输入它在执行检查时将引用的行名称。我已在问题中发布了解决方案。我还质疑您对第1点的理由;它可能对Java有效,但对C#无效。请参阅Yes-correct,因为String==与.Equals的作用相同;我删除了它