GridView使用ObjectDataSource获取错误 [英] Gridview getting error using objectdatasource

查看:85
本文介绍了GridView使用ObjectDataSource获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在gridview中显示为空,我认为问题出在Business Object中.请帮我.我受够了,我从昨晚开始尝试.

在BO.cs中:

命名空间Business_Object
{
    公开类公司:Icompany
    {
        int _empid = 0;

        公共诠释
        {
            放
            {
                如果(值!= _empid)
                    _empid =值;
                
            }
            得到
            {
                返回_empid;
            }
          
        }

        字符串_empname;

        公共字符串empname
        {
            得到
            {
                  返回_empname;
            }
            放
            {
                值= _empname;
            }
        }

        int _jobid;

        公共诠释? Jobid
        {
            得到
            {
                返回_jobid;
            }
            放
            {
                值= _jobid;
            }
        }

        DateTime _hiredate;
        公开DateTime?聘用日期
        {
            得到
            {
                返回_hiredate;
            }
            放
            {
                价值= _hiredate;
            }
        }
    }
}

在BLL.cs中
命名空间BLL
{
    公共班级员工
    {
        公共列表<  公司 >  GetEmployees ()
        {
            公司c =新公司();
            CompanyDataContext db = new CompanyDataContext();
            List <  公司 >  listemp =新列表<  公司 > ( );
            listemp =(来自db.Employees中的p
                       选择新公司{empid = c.empid,empname = c.empname,jobid = c.jobid,hiratedate = c.hiredate}).ToList();
            返回listemp;

        }
    }
}

在default.aspx.cs中
<   asp:GridView     ID   ="   runat   服务器"  DataSourceID    ObjectDataSource1" 
 
                     span>          ="  错误" <   > 
                <   asp:BoundField     DataField   ="   HeaderText    empid "  SortExpression    empid" / <   asp:BoundField     DataField   ="   HeaderText    empname " 
 
                     span>                    SortExpression   ="    > 
                <   asp:BoundField     DataField   ="   HeaderText    jobid "  SortExpression    jobid" / <   asp:BoundField     DataField   ="   HeaderText   已租用" 
 
                     span>                    SortExpression   ="    > 
            <  /列 > 
        
        
        <   asp:ObjectDataSource     ID   ="   runat   服务器"  DataObjectTypeName    BusinessObject.ompany"  TypeName   ="     ="   GetEmployees" <  /company  >  <  /company  >  <  /company  >  

解决方案

您的访问器又回到了前面.

E.G.您已经写了

 DateTime _hiredate;

公共 DateTime?聘用日期
{
    获取
    {
        返回 _hiredate;
    }
    设置
    {
         = _hiredate; // 回到页首
    }
} 



但是你应该有的是

日期时间? _聘用日期;

公共 DateTime?聘用日期
{
    获取
    {
        返回 _hiredate;
    }
    设置
    {
        _hiredate = ;
    }
} 



NB 注意,我还必须将成员变量也设置为可为null的类型-集合访问器要求使用此变量,因为您可能要传入null值

It is displaying empty in gridview, I think problem is in Business Object. Please help me. I am fed up,Ii am trying from last night.

In BO.cs:

namespace Business_Object
{
    public class company:Icompany
    {
        int _empid=0 ;

        public int empid
        {
            set
            {
                if (value != _empid)
                    _empid = value;
                
            }
            get
            {
                return _empid;
            }
          
        }

        string _empname ;

        public string empname
        {
            get
            {
                  return _empname;
            }
            set
            {
                value = _empname;
            }
        }

        int _jobid ;

        public int? jobid
        {
            get
            {
                return _jobid;
            }
            set
            {
                value = _jobid;
            }
        }

        DateTime _hiredate;
        public DateTime? hiredate
        {
            get
            {
                return _hiredate;
            }
            set
            {
                value = _hiredate;
            }
        }
    }
}

in BLL.cs
namespace BLL
{
    public class Employees
    {
        public List<company> GetEmployees()
        {
            company c = new company();
            CompanyDataContext db=new CompanyDataContext();
            List<company> listemp=new List<company>();
            listemp = (from p in db.Employees
                       select new company { empid = c.empid, empname = c.empname, jobid = c.jobid, hiredate = c.hiredate }).ToList();
            return listemp;

        }
    }
}

in default.aspx.cs
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" 

            AutoGenerateColumns="False">
            <columns>
                <asp:BoundField DataField="empid" HeaderText="empid" SortExpression="empid" />
                <asp:BoundField DataField="empname" HeaderText="empname" 

                    SortExpression="empname" />
                <asp:BoundField DataField="jobid" HeaderText="jobid" SortExpression="jobid" />
                <asp:BoundField DataField="hiredate" HeaderText="hiredate" 

                    SortExpression="hiredate" />
            </columns>
        
        
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="BusinessObject.ompany" TypeName="BLL.Employees" SelectMethod="GetEmployees">


        </company></company></company>

解决方案

Your accessors are back to front.

E.G. You have written

DateTime _hiredate;

public DateTime? hiredate
{
    get
    {
        return _hiredate;
    }
    set
    {
        value = _hiredate; // back to front
    }
}



But what you should have is

DateTime? _hiredate;

public DateTime? hiredate
{
    get
    {
        return _hiredate;
    }
    set
    {
        _hiredate = value;
    }
}



N.B. Notice that I have also had to make the member variable a nullable type too - this is required by the set accessor as you may be passing in a null value


这篇关于GridView使用ObjectDataSource获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆