首页 > 解决方案 > 你好。谁能告诉我为什么我的代码 Section='"+lblSection.Text+"' 参数不起作用

问题描述

当我运行它时,它不会填充我的组合框中的项目。

public void FILLREPRESENTATIVE()     
{
  try // fill combobox Party with corresponding Representative Section        
  {                                                                            
    MySqlCommand command = new MySqlCommand("SELECT CONCAT(Firstname,' ',LastName) AS Full_Name, Section, 
    Position FROM candidatetable WHERE Section='"+lblSection.Text+"' AND Position= 'Representative'", connection);

    connection.Open();

    MySqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {            
      cbRepresentative.Items.Add(reader.GetString("Full_Name"));       
    }

    connection.Close();    
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  } 
}

如果我使用这种代码: Section='ict' 它会填满我的组合框。但我想为我的部分使用标签文本。

标签: c#mysql

解决方案


尝试使用这行代码:

MySqlCommand cmd = new MySqlCommand("SELECT CONCAT(Firstname,' ',LastName) AS Full_Name, Section, 
Position FROM candidatetable WHERE Section=@section AND Position= 'Representative'", con);

cmd.Parameters.AddWithValue("@section", lblSection.Text);

推荐阅读