填充組合框我似乎遇到了問題,從我的SQL Server填充一個組合框。不能從SQL Server
至於原因,我已經改變了這個例子,用戶名和密碼,服務器IP,但我已經調試這一點,我知道它連接到服務器。
而且在那裏說
if (reader.HasRows)
{
MessageBox.Show("It has rows!!");
}
它拉過,因爲它有行,但在組合框仍然是空的。
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "123.456.789.101";
builder.InitialCatalog = "DiscoverThePlanet";
builder.UserID = "MadeupUser";
builder.Password = "MadeupPass";
string connectionString = builder.ConnectionString;
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT NoteID, NoteName, Note FROM Notes");
SqlDataReader reader;
cmd.Connection = conn;
reader = cmd.ExecuteReader();
dt.Columns.Add("NoteID", typeof(string));
dt.Columns.Add("NoteName", typeof(string));
dt.Columns.Add("Note", typeof(string));
if (reader.HasRows)
{
MessageBox.Show("It has rows!!");
}
else
{
MessageBox.Show("It doesn't have rows :(");
}
//foreach (DataRow dataRow in dt.Rows)
//{
// foreach (var item in dataRow.ItemArray)
// {
// System.Diagnostics.Debug.Write(item);
// }
//}
noteNamesList.DataContext = dt;
noteNamesList.SelectedValue = "NoteID";
noteNamesList.DisplayMemberPath = "NoteName";
dt.Load(reader);
conn.Close();
}
任何我錯過了或任何幫助將不勝感激。
組合框被命名爲:noteNamesList
嗯,這沒有什麼區別:( –
我沒有足夠的代表評論其他答案,但也許嘗試在分配ComboBox值之前加載您的DataTable?也許在「reader」初始化之前將其移動? – Hank