首页 > 解决方案 > 无法将数据加载到 ComboBox C#

问题描述

我插入数据以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删除前。如果你能帮我解决这个问题,那将是一个很大的帮助。这SelectCategoryComboBox是 的名称ComboBox

标签: c#sqlvisual-studiocombobox

解决方案


我发现出了什么问题...更改以下代码

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"];
                        } 

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

                        }

它对我有用。


推荐阅读