Short one.
A database named ComDet , and column cID(PK) , cName , cDet , mainCate(FK) , Subcat(FK).
一个名为ComDet的数据库,以及列cID(PK),cName,cDet,mainCate(FK),Subcat(FK)。
this suppose to get the data from table ComDet to the combobox..
这假设从表ComDet获取数据到组合框..
DataSet ds2;
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["ComDet"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
but it didnt work.. where did i go wrong? The Data(cName) from database table ComDet is not shown in the combobox. –
但它没有用..我哪里出错了?数据库表ComDet中的数据(cName)未显示在组合框中。 -
2 个解决方案
#1
2
Problem : You are assigning database table name ComDet
as DataSource
but not DataTable
Name daSearch
to ComboBox
.
问题:您将数据库表名称ComDet指定为DataSource,但未将DataTable Name daSearch指定为ComboBox。
Solution : you need to assign valid DataTable
Name to ComboBox
as Datasource
.
解决方案:您需要将有效的DataTable Name作为数据源分配给ComboBox。
Replace This :
替换这个:
ListU.DataSource = ds2.Tables["ComDet"];
With This:
ListU.DataSource = ds2.Tables["daSearch"];
(or)
ListU.DataSource = ds2.Tables[0];
Complete Code:
DataSet ds2;
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["daSearch"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
#2
1
So the question asks how to get data from database to combobox. Personally, I find using the DataSet
class to be unpreferable - it is quite prone to errors like the asker is experiencing here.
所以问题是如何从数据库获取数据到组合框。就个人而言,我发现使用DataSet类是不可取的 - 它很容易出现像提问者在这里遇到的错误。
Try this approach. Which reads all the cName
into a List
and binds the List
to the ComboBox
. Simple and readable code. The use of using
statements also ensures that the unmanaged resources are released efficiently.
试试这种方法。它将所有cName读入List并将List绑定到ComboBox。简单易读的代码。 using语句的使用还可确保有效释放非托管资源。
private void searchBtn_Click(object sender, EventArgs e)
{
var list = new List<string>();
using (var conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
using (var cmd = new SqlCommand("SELECT cName FROM ComDet", conn))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(Convert.ToString(reader["cName"]));
}
}
}
}
ListU.DataSource = new BindingSource(list, null);
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
#1
2
Problem : You are assigning database table name ComDet
as DataSource
but not DataTable
Name daSearch
to ComboBox
.
问题:您将数据库表名称ComDet指定为DataSource,但未将DataTable Name daSearch指定为ComboBox。
Solution : you need to assign valid DataTable
Name to ComboBox
as Datasource
.
解决方案:您需要将有效的DataTable Name作为数据源分配给ComboBox。
Replace This :
替换这个:
ListU.DataSource = ds2.Tables["ComDet"];
With This:
ListU.DataSource = ds2.Tables["daSearch"];
(or)
ListU.DataSource = ds2.Tables[0];
Complete Code:
DataSet ds2;
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["daSearch"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
#2
1
So the question asks how to get data from database to combobox. Personally, I find using the DataSet
class to be unpreferable - it is quite prone to errors like the asker is experiencing here.
所以问题是如何从数据库获取数据到组合框。就个人而言,我发现使用DataSet类是不可取的 - 它很容易出现像提问者在这里遇到的错误。
Try this approach. Which reads all the cName
into a List
and binds the List
to the ComboBox
. Simple and readable code. The use of using
statements also ensures that the unmanaged resources are released efficiently.
试试这种方法。它将所有cName读入List并将List绑定到ComboBox。简单易读的代码。 using语句的使用还可确保有效释放非托管资源。
private void searchBtn_Click(object sender, EventArgs e)
{
var list = new List<string>();
using (var conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
using (var cmd = new SqlCommand("SELECT cName FROM ComDet", conn))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(Convert.ToString(reader["cName"]));
}
}
}
}
ListU.DataSource = new BindingSource(list, null);
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}