錯誤:'WinWithStudentDatabase.Broker.FillComboBox()':並非所有代碼 路徑都返回一個值。並非所有的代碼路徑都返回一個值sql
我知道那是什麼錯誤的手段,但不能弄清楚爲什麼它不工作:/ ...這是我的代碼:
public List<Person> FillComboBox()
{
List<Person> personsList = new List<Person>();
try
{
string sql = "SELECT * FROM Tperson";
cmd = new SqlCommand(sql, connection);
connection.Open();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read() != null)
{
Person p = new Person();
p.Id = Convert.ToInt32(reader["ID"].ToString());
p.FirstName = reader["FirstName"].ToString();
p.LastName = reader["LastName"].ToString();
personsList.Add(p);
}
return personsList;
}
catch (Exception eX)
{
MessageBox.Show(eX.Message);
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}
有什麼建議?我嘗試剛開始讀DB數據和填充組合框就是所有..
噢IM笨蛋的感謝!其工作正常 – franzp
如果出現錯誤,它還能正常工作嗎?調用代碼是否處理返回的'null'? – CodeCaster