0
我有员工班级和员工列表,如下所示,但我获得空白的ASP.NET页面。没有GridView显示,我也没有发现任何错误消息。 我在做什么?
创建班级Employee并使用SELECT获取所有员工到GridView
public class Employee
{
public int EmployeeId { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
}
而且employeeelist
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();
}
ATT最后我的GridView:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
预先感谢您!
显然事情错了。在每个记录读取后放置一个'Debug.Print',看看在调试会话期间输出中是否有任何东西 – fnostro
@fnostro,谢谢Fnostro ...我发现了我自己的misstake ..;)我已经使用了tostring()之后每一个像emp.FirstName = reader [“FirstName”]。ToString(); –
非常欢迎。 – fnostro