下面这几个,是在实际开发或阅读中发现的一些问题,有些甚至是有很多年开发人员写出的代码,也是很多人经常犯的错误。各位可以看看,你有没有躺着中枪。
一、用static来保持页面回发
static int id;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ID"] != null && Request.QueryString["ID"].ToString() != "")
{
id = Convert.ToInt32(Request.QueryString["ID"].ToString());
}
}
二、用编程方式绑定数据控件时,数据源为DataSet时判断null而不判读DataSet内的Tables数。
DataSet ds = bll.GetList();
if (ds != null && ds.Tables.Count > 0)
{
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
//或
DataSet ds = bll.GetList()??new DataSet();
if (ds.Tables.Count > 0)
{
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
三、用编程方式绑定数据控件时,数据源为DataTable或List<T>时判断null。
DataTable dt = bll.GetList();
if (dt!=null)
{
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
四、
string txtName = Request["txtName"] == null ? "" : Request["txtName"].ToString();
string strWhere += "and ID=" + userId + ""; //userId是int
if (txtName != "")
{
strWhere += " and NAME='" + txtName + "'";
}
strWhere += " order by id desc";
//项目本身都是采用参数化查询的,这里是一些暴露给Web层的高级查询条件。
五、对整型变量进行非null判断。
// a 是int型 (不是int?)
if(a != null){
//操作
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。