我的Windows窗体应用程序中有一个comboBox和一个checkListBox,它连接到我的SQL数据库。我得到了绑定数据部分,但我不知道如何在选择comboBox项时在checkListBox中显示数据。假设我的comboBox中有10个与我的SQL数据库绑定的项目,它们位于列名(“应用程序名称”)下,例如excel,word,android,eclipse等....我在表单开始时调用此方法载入。对不起长代码。
以下是applicationComboBox
private void loadComboBox()
{
myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
try
{
myConn.Open();
//my table name is Application_Detail
string query = "select * from Application_Detail";
myCommand = new SqlCommand(query, myConn);
//reading the value from the query
SqlDataReader dr = myCommand.ExecuteReader();
//Reading all the value one by one
while (dr.Read())
{
//column is 1 in Application_Detail Data
//GetString(1) display the 2nd column of the table
string name = dr.GetString(1);
//display the application name in column 2 -
applicationComboBox.Items.Add(name);
}
myConn.Close();
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这部分代码的结果是:
//label Name //Application Name
Application Name:
Excel
Word
NotePad
PowerPoint
SubLime
Eclipse
在我调用此方法后,我想根据用户在此applicationComboBox
中选择的内容显示教师姓名。因此,如果教师1,2,3使用Excel并且用户从comboBox中选择了excel,则checkListBox将显示教师1,2,3,反之亦然。为此,我在comboBox1_SelectedIndexChanged
方法调用方法,因为我想在从comboBox中选择项目时显示详细信息。以下是我的代码
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//I check if the comboBox index is at 0, it disable the button.
if (applicationComboBox.SelectedIndex == 0)
{
exportButton.Enabled = false;
this.teacherCheckListBox.DataSource = null;
teacherCheckListBox.Items.Clear();
}
//it it is not at 0,
else
{
exportButton.Enabled = true;
//call this method
fill_checkListBox();
}
//teacherCheckListBox
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void fill_checkListBox()
{
myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
try
{
myConn.Open();
//for reading purpose, I break down by long statement
//In this statement, I left join 3 table (Teacher_Detail, AppUser_Detail, and Application_Detail table). My AppUser_Detail contains all 3 id (teacherId, applicationId, and AppUserId). I then set filter the table using `where` keyWord to make the applicationId = the comboBox text
string query = "SELECT
td.chineseName,
ad.applicationId,
aud.applicationId,
ad.applicationName
FROM[AppUser_Detail] as aud
LEFT JOIN[Teacher_Detail] as td
ON aud.teacherId = td.teacherId
LEFT JOIN[Application_Detail] as ad
ON aud.applicationId = ad.applicationId
where aud.applicationId = '" + applicationComboBox.Text + "' AND NOT(td.teacherId IS NULL)
";
myCommand = new SqlCommand(query, myConn);
//reading the value from the query
SqlDataReader dr = myCommand.ExecuteReader();
//Reading all the value one by one
while (dr.Read())
{
//column is 0 where the teacherName belong in my Teacher_Detail table
string name = dr.GetString(0);
//I tried to set the text of the checkListBox as the teacherName, but I can't somehow
teacherCheckListBox.Text = name;
}
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
当我运行这样的程序时,它说Conversion failed when converting the varchar value "Excel" to data type int.
有没有办法解决它?它应该不是问题,因为在我的Application_Detail表中,我的applicationName和我的teacherName的数据类型被设置为nvarchar(50)和applicationId和teacherId = int;
答案 0 :(得分:0)
问题出在这条线上,我想:
where aud.applicationId = '" + applicationComboBox.Text +
基于你的代码,我认为applicationId是一个int,applicationComboBox.Text就是那个文本。
试试这个:
where ad.applicationName = '" + applicationComboBox.Text.Trim() +
试试这个:
if (string.IsNullOrWhiteSpace(teacherCheckListBox.FindString(name))
{
teacherCheckListBox.Items.Add(name);
}