无法将数据加载到ComboBox C#

问题描述 投票:0回答:2

我使用ComboBox中的以下代码将数据插入到FormLoad Block中>

try
{
    using (SqlConnection con = new SqlConnection(conString))
    {
        SelectCategoryComboBox.Items.Clear();
        string query = "SELECT CategoryName FROM CategoryTable";
        con.Open();
        SqlDataReader sdr = new SqlCommand(query, con).ExecuteReader();
        while (sdr.Read())
        {
            SelectCategoryComboBox.Items.Add(sdr.GetValue(0).ToString());
        }

    }
}
catch
{
    StatusLabel.Text = "An error occured while loading Data";
}
finally
{
    SelectCategoryComboBox.SelectedItem = null;
    SelectCategoryComboBox.SelectedText = "Choose Category";
}

它完成工作。在表格中,您可以创建类别并通过从ComboBox Here is a ScreenShot the Form中选择类别的名称来删除。我使用以下代码删除了项目,然后将其加载到ComboBox中。

try
{
    String conString = ConfigurationManager.ConnectionStrings["mfcdb"].ConnectionString;
    String query = "DELETE FROM CategoryTable WHERE CategoryName='" + SelectCategoryComboBox.SelectedItem.ToString() + "'";
    using (SqlConnection con = new SqlConnection(conString))
    {
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        cmd.ExecuteNonQuery();
    }
    StatusLabel.Text = "You have successfully deleted " + SelectCategoryComboBox.SelectedItem.ToString() + " Category";
}
catch
{
    StatusLabel.Text = "An Error occured while deleting " + SelectCategoryComboBox.SelectedItem.ToString() + " Category";
}
finally
{
    try
    {
        SelectCategoryComboBox.Items.Clear();
        String conString = ConfigurationManager.ConnectionStrings["mfcdb"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conString))
        {


            string query = "SELECT CategoryName FROM CategoryTable";
            con.Open();
            SqlDataReader sdr = new SqlCommand(query, con).ExecuteReader();
            while (sdr.Read())
            {
                SelectCategoryComboBox.Items.Add(sdr.GetValue(0).ToString());
            }

        }
    }
    catch
    {
        StatusLabel.Text = "An error occured while loading Data";
    }
    finally
    {
        SelectCategoryComboBox.SelectedItem = null;
        SelectCategoryComboBox.SelectedText = "Choose Category";
    }

下面创建新项目的代码

 if (CategoryNameText.Text == "")
            {
                StatusLabel.Text = "You have to provide a name to create a category";
            }
            else
            {
                String conString = ConfigurationManager.ConnectionStrings["mfcdb"].ConnectionString;
                String query = "INSERT INTO CategoryTable(CategoryName) VALUES('" + CategoryNameText.Text + "')";
                try
                {
                    using (SqlConnection con = new SqlConnection(conString))
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand(query, con);
                        cmd.ExecuteNonQuery();
                    }
                    StatusLabel.Text = "You have successfully created " + CategoryNameText.Text + " Category";

                    try
                    {
                        using (SqlConnection scon = new SqlConnection(conString))
                        {
                            string locQuery = "SELECT CategoryName,Categoryid FROM CategoryTable";
                            SqlDataAdapter da = new SqlDataAdapter(locQuery, scon);
                            scon.Open();
                            DataSet ds = new DataSet();
                            da.Fill(ds, "CategoryTable");
                            SelectCategoryComboBox.ValueMember = "Categoryid";
                            SelectCategoryComboBox.DisplayMember = "CategoryName";
                            SelectCategoryComboBox.DataSource = ds.Tables["CategoryTable"];
                        }
                    }
                    catch
                    {
                        Thread.Sleep(3000);
                        StatusLabel.Text = "An Error Occured while Loading Data!";
                    }
                    finally
                    {
                        SelectCategoryComboBox.SelectedItem = null;
                        SelectCategoryComboBox.SelectedText = "Choose Category";
                    }
                    CategoryNameText.Focus();
                }
                catch
                {
                    Thread.Sleep(3000);
                    StatusLabel.Text = ("An ERROR occured While creating category!");
                }
                finally
                {
                    CategoryNameText.Text = "Enter Category Name";
                }

            }
        }

此代码完美地删除了项目。但是,如果我删除了ComboBox中已经存在的项目,它将执行作业,即删除并将剩余项目加载到ComboBox中。但是,如果我创建了一个项目并删除了它在关闭表单之前会删除该项目。但是无法加载其余项目。它显示删除前ComboBox中已经存在的所有项目。如果您能帮助我解决这个问题,将是一个很大的帮助。 SelectCategoryComboBoxComboBox的名称。

我在FormLoad块中使用以下代码将数据插入了ComboBox尝试{使用(SqlConnection con = new SqlConnection(conString)){SelectCategoryComboBox.Items.Clear(); ...

c# sql visual-studio combobox
2个回答
0
投票

我发现问题出现在下面的句子中。


0
投票

我发现哪里出了问题...更改后续代码

© www.soinside.com 2019 - 2024. All rights reserved.