【发布时间】:2017-04-25 07:05:05
【问题描述】:
如下所示,我有员工类和员工列表,但我得到空白的 ASP.NET 页面。没有显示 GridView,我也没有收到任何错误消息。
我在做什么错了?
public class Employee
{
public int EmployeeId { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
}
还有员工列表
public List<Employee> GetEmployeeList()
{
try
{
string CS = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
empList = new List<Employee>();
string sqlSelectString = "SELECT * FROM Employee";
command = new SqlCommand(sqlSelectString, conn);
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Employee emp = new Employee();
emp.EmployeeId = reader.GetInt32(reader.GetOrdinal("ID"));
emp.FirstName = reader.GetString(reader.GetOrdinal("FirstName"));
emp.LastName = reader.GetString(reader.GetOrdinal("LastName"));
empList.Add(emp);
}
command.Connection.Close();
return empList;
}
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
return null;
}
然后在pageLoading中
protected void Page_Load(object sender, EventArgs e)
{
Employee employee = new Employee();
List<Employee> employeeList;
employeeList = employee.GetEmployeeList();
GridView1.DataSource = employeeList;
GridView1.DataBind();
}
最后我的 Gridview:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
提前谢谢你!
【问题讨论】:
-
显然出了点问题。在每次读取记录后放置一个
Debug.Print,并查看在调试会话期间输出中是否出现任何内容 -
@fnostro ,谢谢 Fnostro ......我发现了自己的错误...... ;) 我让你在每个喜欢后使用 tostring() emp.FirstName = reader["FirstName"].ToString() ;
-
不客气。
标签: c# asp.net gridview data-access-layer