将空值放在对象列表中

时间:2016-04-01 09:47:01

标签: c# asp.net entity-framework oop gridview

我有一个用于从数据库获取数据的类和一个webform以在gridview中显示它们。这是我的班级:

public class Personel
{
    dincerelektrnik_AracTakipEntities entity = new dincerelektrnik_AracTakipEntities();

    public Personel(int DriverId, string DriverName, string DriverSurname, string DriverPosition, 
        bool DriverGender, string DriverImage, string DriverEmail,string Password, 
        DateTime? DriverBirthDate)
    {
        this.DriverId = driverId;
        this.DriverName = driverName;
        this.DriverSurname = driverSurname;
        this.DriverPosition = driverPosition;
        this.Gender = gender;
        this.DriverImage = driverImage;
        this.DriverEmail = driverEmail;
        this.Password = password;
        this.BirthDate = birthDate;
    }

    private int driverId;
    public int DriverId
    {
        get { return driverId; }
        set { driverId = value; }
    }

    private string driverName;
    public string DriverName
    {
        get { return driverName; }
        set { driverName = value; }
    }

    private string driverSurname;
    public string DriverSurname
    {
        get { return driverSurname; }
        set { driverSurname = value; }
    }

    private string password;
    public string Password
    {
        get { return password; }
        set { password = value; }
    }

    private DateTime? birthDate;
    public DateTime? BirthDate
    {
        get {
            if (birthDate.HasValue)
                return birthDate;
            else
                return DateTime.Now.Date;
        }
        set { birthDate = value; }
    }

    private string driverPosition;
    public string DriverPosition
    {
        get { return driverPosition; }
        set { driverPosition = value; }
    }

    private bool gender;
    public bool Gender
    {
        get { return gender; }
        set { gender = value; }
    }

    private string driverImage;
    public string DriverImage
    {
        get { return driverImage; }
        set { driverImage = value; }
    }

    private string driverEmail;
    public string DriverEmail
    {
        get { return driverEmail; }
        set { driverEmail = value; }
    }

    public List<Personel> GetAllDrivers() 
    {
        List<Personel> personeller = new List<Personel>();

        var drivers = entity.prGetDrivers();
        foreach (var driver in drivers) 
        {
            personeller.Add(new Personel(driver.DriverId, driver.DriverName, driver.DriverSurname,
                driver.DriverPosition, driver.DriverGender, driver.DriverImage, driver.DriverEmail,
                driver.Password, driver.DriverBirthDate));
        }
        return personeller;
    }
}

我的网格视图:

<asp:GridView ID="GridViewPersoneller" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Ad/Soyad">
                    <ItemTemplate>
                        <%#Eval("DriverName") %>&nbsp;<%#Eval("DriverSurname") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%#Eval("DriverEmail") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%#Eval("DriverBirthDate") %>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>

webform的代码隐藏:

Personel personel = new Personel(1, "deneme", "deneme", "deneme", true, "deneme", "deneme", "deneme", DateTime.Now.Date);

    protected void Page_Load(object sender, EventArgs e)
    {
        FillPeronselGridView();
    }

    protected void FillPeronselGridView() 
    {
        List<Personel> personelList = personel.GetAllDrivers();
        GridViewPersoneller.DataSource = personelList;
    }

当我运行代码时,我什么也看不见。不是错误或gridview。当我调试时,我看到我用我的存储过程得到所有数据并将它们存储在&#39; drivers&#39;变量没有问题,但是当它在对象列表中添加它时,它会添加空值。为什么会发生这种情况,我该如何解决这个问题。感谢您的时间和支持。

1 个答案:

答案 0 :(得分:1)

问题是,在构造函数中,您使用Titlecase定义了变量,但在初始化属性时,您使用了类字段。

修改代码以使用传递的参数而不是类变量。

你的构造函数应该是。

    public Personel(int DriverId, string DriverName, string DriverSurname, string DriverPosition,
        bool DriverGender, string DriverImage, string DriverEmail, string Password,
        DateTime? DriverBirthDate)
    {
        this.DriverId = DriverId;
        this.DriverName = DriverName;
        this.DriverSurname = DriverSurname;
        this.DriverPosition = DriverPosition;
        this.Gender = DriverGender;
        this.DriverImage = DriverImage;
        this.DriverEmail = DriverEmail;
        this.Password = Password;
        this.BirthDate = DriverBirthDate;
    }