[英]How to retrieve all the column values(distinct had been applied) into listbox by using sql query?
This might be simple one and also this has been answered before but I don't know how to look for the exact method. 这可能很简单,而且之前已经回答过,但是我不知道如何寻找确切的方法。
-> Am having two list box in my form. ->我的表单中有两个列表框。 One is for product name and other one is for Company name.
一种是产品名称,另一种是公司名称。
-> On page load, Listbox1
will retrieve values from database(Product name)
. ->在页面加载时,
Listbox1
将从database(Product name)
检索值。 Once item has been selected from listbox1
, the respective company names should be fetched in listbox2
. 从
listbox1
选择项目后,应在listbox2
获取相应的公司名称。
For eg : Database name is Motor 例如 :数据库名称是Motor
Here is my table named as "Register" and it contains two columns they are, 这是我的表格,名称为“ Register”,其中包含两列,
Productname Companyname
Car Bmw
Bike Bmw
Car Honda
Bike Honda
My Question is 我的问题是
I retrieved product name details into listbox1
, here the thing is i don't want to repeat the same items, so I used Distinct like this, 我将产品名称详细信息检索到
listbox1
,这是我不想重复相同的项目,所以我像这样使用Distinct,
Select Distinct Productname from Register
Now if i select car from listbox 1 then the respective company names should be display in listbox2. 现在,如果我从列表框1中选择汽车,则相应的公司名称应显示在列表框2中。 But what am getting is only Honda on my listbox2 and am not getting BMW.
但是得到的只是本田车上的本田而没有得到宝马。
Sqlcommand cmd=new Sqlcommand("Select Companyname from Register where Productname='"+listbox1.selecteditem+"'",con);
Sqldatareader dr=cmd.Executereader();
while(dr.Read())
{
string a=dr.GetString(0);
listbox2.items.add(a);
}
This is the query which is used to retrieve values into listbox2. 这是用于将值检索到listbox2中的查询。 I used datareader to read and get the values from the database.
我使用datareader读取并从数据库中获取值。
I don't know exactly how to do this. 我不知道该怎么做。 Hope am not confusing you.So any help would be more helpful to me and thanks in advance.
希望不要让您感到困惑,因此任何帮助都会对我有所帮助,并在此先感谢您。
Have you tried with, 你试过了吗
Select Companyname from Register
where Productname='"+listbox1.GetItemText(listbox1.SelectedItem)+"'
Try this.. 尝试这个..
Sqlcommand cmd=new Sqlcommand("Select Companyname from Register where Productname='"+listbox1.selecteditem.toString()+"'",con);
Sqldatareader dr=cmd.Executereader();
listbox2.items.clear();
while(dr.Read())
{
string a=dr['Companyname'].toString();
listbox2.items.add(a);
}
ur using string a=dr.GetString(0); ur使用字符串a = dr.GetString(0); listbox2.items.add(a);
listbox2.items.add(a);
which will fetch only first row from table.. 这将只从表中获取第一行。
use string a=dr["companyname"].toString(); 使用字符串a = dr [“ companyname”]。toString(); listbox2.items.add(a);
listbox2.items.add(a);
this should solve your problem. 这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.