我写了个winform的用户控件,里面调用了用企业库写的dal方法,比如getNamebyId之类的,然后编后后拖入其他winform窗口里面报错,怎么回事?

解决方案 »

  1.   

    用户控件
    要注意作 [设计时不作数据库连接处理=.]的排除操作.如:在控件的某属性前加
    if (this.Site !=null && this.Site.DesignMode) return null ;
      

  2.   

    报错的图用户控件测试的代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using Haoting.DAL;
    using Haoting.BLL;namespace Haoting.UI
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
                ProjectManager pm = new ProjectManager();
                label1.Text = pm.GetById("{8676E22F-1B62-4760-BF33-B8D44D4BB634}").Tables[0].Rows[0][0].ToString();
            }
        }
    }
      

  3.   

    http://www.cnblogs.com/images/cnblogs_com/emilchan/121285/r_userCerr.jpg
    贴了两次都贴不上图,太不人性化了
      

  4.   

    如果在设计Form时并不需要getid()的某些操作,
    那么在getid()前加上
    if   (this.Site   !=null   &&   this.Site.DesignMode)   return   null   ; 
      

  5.   

    又是未将对象引用到实例问题,肯定是你用户控件的某个方法返回值为空,然后你还读取返回值了。
    比如:
     .....略......
       SqlDataReader dr=objSqlCommand.ExecuteReader();
       dr.Read();
    如果dr为空 那么dr.getstring()什么的都会报未将对象引用到实例问题,空指针异常。