下拉列表中不SqlDataReader的结合 [英] Drop down list not binding with sqldatareader
问题描述
我有大约五滴的集合下来的一种形式。我有我的查询,如下所示。
i have a form with a collection of about five drop down . i have my query as follows .
string sql = "SELECT a.clientID ,a.[cname],b.bid,b.[bname],c.contactID, c.[name] FROM "
+ " dbo.[CLIENT] AS a INNER JOIN dbo.[BRANCH] AS b "
+ "ON a.clientID = b.clientID JOIN dbo.[CONTACT] AS "
+ " c ON b.bid = c.bid ORDER BY a.clientID ";
然后我也跟着上下单独绑定我下降到其相应列如下:
i then followed and bind my drop down individually to their respective columns as follows.
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
SqlDataReader reader = cmd.ExecuteReader();
drClient.Enabled = true;
drClient.DataSource = reader;
drClient.DataTextField = "cname";
drClient.DataValueField = "clientID";
drClient.DataBind();
drBranch.Enabled = true;
drBranch.DataSource = reader;
drBranch.DataTextField = "bname";
drBranch.DataValueField = "bid";
drBranch.DataBind();
drContact.Enabled = true;
drContact.DataSource = reader;
drContact.DataTextField = "name";
drContact.DataValueField = "contactID";
drContact.DataBind();
drEmail.Enabled = true;
drEmail.DataSource = reader;
drEmail.DataTextField = "name";
drEmail.DataValueField = "contactID";
drEmail.DataBind();
drFax.Enabled = true;
drFax.DataSource = reader;
drFax.DataValueField = "contactID";
drFax.DataTextField = "name";
drFax.DataBind();
当我运行这一点,只有第一个下拉绑定成功。其余的则没有。我也尝试通过读者回路中加入
when i run this, only the first drop down bind successfully. The rest don't. I also try to loop through the reader by adding
while(reader.read())
{
then my bindings
}
上面也会失败。我虽然如下循环为好。
the above also fails. I though of looping as below as well.
while(read.HasRows)
{
}
仍然失败。我很困惑,任何帮助将是AP preciated。谢谢
it still fails. I am confused,any help would be appreciated. thanks
推荐答案
阅读器是只读
键,只向前,这就是为什么只有第一个 dropdonw
得到填补数据和其他都是空的。
您可以使用 datset
或数据表
的同样的问题。
Reader is readonly
and forward only that's why only first dropdonw
get filled with data and others are empty.
You can use datset
or Datatable
for same problem .
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
Dataset dsresult = cmd.ExecuteDataset();
If(dsResult !=null)
{
if(dsResult.Rows.count>0)
{
drClient.Enabled = true;
drClient.DataSource = dsResult.Tables[0] ;
drClient.DataTextField = Convert.ToString(ds.Tables[0].Columns["cname"]);
drClient.DataValueField = ds.Tables[0].Columns["clientID"] ;
drClient.DataBind();
}
}
的DataReader
连接架构需要持续连接,并在转发模式一次获取一行更好的使用集
这用途断开架构
和可用于检索数据多次
Datareader
is connected architecture needs continuous connection and fetches one row at a time in forward mode better use dataset
which uses disconnected architecture
and can be used for retrieving data multiple times.
这篇关于下拉列表中不SqlDataReader的结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!