[英]Not able to display column entries using Data Reader
SqlConnection con = new SqlConnection("My path");
SqlCommand cmd;
SqlDataReader dr;
Label1.Text = Session["name"].ToString();
con.Open();
cmd = new SqlCommand("Select '"+Session["name"]+"' from table1",con);
dr = cmd.ExecuteReader();
while(dr.read()){
Dropdownlist1.Items.Add(); ------->Stuck here
}
I am selecting a column from a table which will have 3 or more entries. 我正在从具有3个或更多条目的表中选择一列。 And I want to put them in dropdownlist.
我想将它们放在下拉列表中。 I tried different approaches but nothing is giving me output I require.
我尝试了不同的方法,但没有提供所需的输出。 It shows error or display first entry only.
它显示错误或仅显示第一个条目。
Please feel free to edit my code and provide a suitable idea. 请随时编辑我的代码并提供合适的想法。
THANKS 谢谢
This is my table: 这是我的桌子:
SKY | SEA | LAND
-----------------------
EAGLE| SHARK | LION
CROW | FISH | TIGER
DUCK | WHALE | DEER
Where Session["name"]
can be SKY/SEA/LAND and depending on that I want my drop down to show the entries below it IF SKY is Session["name"] then dropdwonlist should display EAGLE CROW DUCK 在
Session["name"]
可以是SKY / SEA / LAND的情况下,并且如果希望SKY是Session [“ name”],则我希望下拉菜单显示下面的条目,然后dropdwonlist应该显示EAGLE CROW DUCK
Have you tried? 你有没有尝试过?
while (dr.read()){
Dropdownlist1.Items.Add(dr["Name"].ToString());
}
If you want to go get only once and without also having to specify the column name then you could use GetString(0) like: 如果您只想获取一次而不必指定列名,则可以使用GetString(0),例如:
while (dr.read()){
Dropdownlist1.Items.Add(dr.GetString(0));
break;
}
Base on your question you are passing Session["name"] which is your table Column Name to your select query. 根据您的问题,您正在将Session [“ name”](这是您的表列名)传递给select查询。
There is also need to change on your code: 还需要更改您的代码:
Lets say your Session["name"] value is SKY 假设您的Session [“ name”]值为SKY
"Select '"+Session["name"]+"' from tableA" --session["name"] is treated here as a string
this will be rendered as Select 'Session[Name]' from TableA
which is incorrect. 这将呈现为
Select 'Session[Name]' from TableA
错误。 so the result will be : 因此结果将是:
Session[Name]
Session[Name]
Session[Name]
Remove the single quote on the query and let's say : Session["name"] value is SKY 删除查询上的单引号,然后说:Session [“ name”]的值为SKY
cmd = new SqlCommand("Select "+Session["name"].ToString()+" from table1",con);
the query will be -> Select SKY from table1
查询将是->
Select SKY from table1
and the select result 和选择结果
EAGLE
CROW
DUCK
If so try this : 如果是这样,请尝试以下操作:
SqlConnection con = new SqlConnection("My path");
SqlCommand cmd;
SqlDataReader dr;
Label1.Text = Session["name"].ToString();
con.Open();
cmd = new SqlCommand("Select "+Session["name"].ToString()+" from table1",con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
// get the results of each column
Dropdownlist1.Items.Add(dr[0].ToString()); //this will get your first column even your Session["name"] will contain any column values
}
Check also this tutorial: Lesson 04: Reading Data with the SqlDataReader 还要检查本教程: 第04课:使用SqlDataReader读取数据
Best Regards 最好的祝福
you should use like below 你应该像下面这样使用
while (dr.read()){
ListItem l = new ListItem(dr["name"].ToString(), "value", true);
Dropdownlist.Items.Add(l);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.