【发布时间】:2014-04-02 13:09:31
【问题描述】:
错误:'WinWithStudentDatabase.Broker.FillComboBox()':并非所有代码 路径返回一个值。
我知道那个错误是什么意思,但不知道为什么它不起作用:/ ...这是我的代码:
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();
}
}
}
有什么建议吗?我试图从我的数据库中读取数据并填充组合框,仅此而已..
【问题讨论】:
标签: c# sql-server winforms error-handling path