C# 无法将下拉菜单中的项目插入我的数据库

C# 无法将下拉菜单中的项目插入我的数据库,c#,mysql,visual-studio,C#,Mysql,Visual Studio,我是C语言的新手,我正在尝试将一些值插入到我在VisualStudio中创建的数据库中 -我正在创建一个配方应用程序- 因此,在表单中,我有一些组件,例如标题、配料、说明的文本框,一个下拉项目组合框,用于指定是食物还是甜食,以及一个按钮,用于将所有这些数据插入我的数据库 当我按下按钮时,我可以将所有文本框添加到数据库中,除了下拉值 这是按钮中的代码 private void addItemButton_Click(object sender, EventArgs e) {

我是C语言的新手,我正在尝试将一些值插入到我在VisualStudio中创建的数据库中

-我正在创建一个配方应用程序- 因此,在表单中,我有一些组件,例如标题、配料、说明的文本框,一个下拉项目组合框,用于指定是食物还是甜食,以及一个按钮,用于将所有这些数据插入我的数据库

当我按下按钮时,我可以将所有文本框添加到数据库中,除了下拉值

这是按钮中的代码

       private void addItemButton_Click(object sender, EventArgs e)
    {

        string dat = "Insert into [Table](Title,Category,Ingredients,Description) Values('" + titleTextBox.Text + "','" + dropdownCategory.SelectedValue + "','" + addIngredientTextBox.Text + "','" + addDescriptionTextBox.Text + "')";
        SqlConnection sqlCon = new SqlConnection(connectionString);
        SqlCommand sqlCmd = new SqlCommand(dat, sqlCon);
        sqlCon.Open();
        sqlCmd.ExecuteNonQuery();
        sqlCon.Close();
    }

我将尝试研究字符串dat的内容。 如果dropdownCategory.SelectedValue返回某些内容,则可能包含无效数据 你没想到的

除此之外,Open、ExecuteOnQuery和Close方法的顺序可能不正确。尝试阅读这些方法的文档


另一件事,你应该看看是返回的错误消息,如果有一个。它们通常信息量很大。

好吧,我不知道它是否正确,但它对我起了作用

我变了

dropdownCategory.SelectedValue


我做了一个代码示例,它可以成功地将combobox值插入数据库

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dat = string.Format("Insert into [Sample](Title,Category,Ingredients,Description)values('{0}','{1}','{2}','{3}')", textBox1.Text, comboBox1.SelectedItem,textBox2.Text,textBox3.Text);
            string connectionString = @"connectionstring";
            SqlConnection sqlCon = new SqlConnection(connectionString);
            SqlCommand sqlCmd = new SqlCommand(dat, sqlCon);
            sqlCon.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCon.Close();
            MessageBox.Show("success");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(new object[] { "basketball","football", "volleyball" });

        }
    }

我将MessageBox.ShowdropdownCategory.SelectedValue.ToString;要检查消息,它会给我以下错误:System.NullReferenceException:“对象引用未设置为对象的实例。”System.Windows.Forms.ListControl.SelectedValue.get返回null。下拉列表似乎不包含值。我将添加空检查。类似于dropdownCategory.SelectedValue=无效的dropdownCategory.SelectedValue:默认值;应该有用。
  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dat = string.Format("Insert into [Sample](Title,Category,Ingredients,Description)values('{0}','{1}','{2}','{3}')", textBox1.Text, comboBox1.SelectedItem,textBox2.Text,textBox3.Text);
            string connectionString = @"connectionstring";
            SqlConnection sqlCon = new SqlConnection(connectionString);
            SqlCommand sqlCmd = new SqlCommand(dat, sqlCon);
            sqlCon.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCon.Close();
            MessageBox.Show("success");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(new object[] { "basketball","football", "volleyball" });

        }
    }