-1
我無法找到問題出在哪裏,當我把while循環,因爲它工作的意見,之後while循環,我們不能從SqlDataReader的更多閱讀? listbox2仍然是空的!爲什麼listbox2仍然是空的?
public partial class exTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
string sql = "select employeeid,firstname,lastname from employees";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
string item;
ListBox1.Items.Clear();
while (reader.Read())
{
item =reader.GetString(1) + reader.GetString(2);
ListBox1.Items.Add(item);
}
ListBox2.DataSource = reader;
ListBox2.DataValueField = "employeeId";
ListBox2.DataTextField = "firstname";
ListBox2.DataBind();
reader.Close();
con.Close();
}
}
添加來自閱讀器的項目列表,而不是設置數據源給讀者。我認爲這個問題是'reader'是一個迭代器,並且你已經遍歷一次了,所以你的時間設置源給讀者,它沒有留下任何物品。 –
'ListBox2.DisplayMember = 「姓名」; ListBox2.ValueMember =「姓氏」;'試試..還怎麼可能僱員永遠是一個使用時,它不是你原來的選擇查詢 'ListBox2.DataSource =讀者的一部分;'應設置最後 – MethodMan
emloyeeid是查詢的一部分在字符串sql中,Iam仍然困惑 –