报错的图用户控件测试的代码 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(); } } }
要注意作 [设计时不作数据库连接处理=.]的排除操作.如:在控件的某属性前加
if (this.Site !=null && this.Site.DesignMode) return null ;
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();
}
}
}
贴了两次都贴不上图,太不人性化了
那么在getid()前加上
if (this.Site !=null && this.Site.DesignMode) return null ;
比如:
.....略......
SqlDataReader dr=objSqlCommand.ExecuteReader();
dr.Read();
如果dr为空 那么dr.getstring()什么的都会报未将对象引用到实例问题,空指针异常。