未将对象引用设置到对象的实例
这个应该怎么改?protectedvoidPage_Load(objectsender,EventArgse){stringconnstr=ConfigurationMa...
这个应该怎么改?
protected void Page_Load(object sender, EventArgs e)
{string connstr =ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection objCon = new SqlConnection(connstr);
objCon.Open();
string strSql = "select quanxian from [user] where name='" + Session["name"] + "'";
SqlCommand objCmd = new SqlCommand(strSql, objCon);
string quanxian= (string)objCmd.ExecuteScalar().ToString();
} 展开
protected void Page_Load(object sender, EventArgs e)
{string connstr =ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection objCon = new SqlConnection(connstr);
objCon.Open();
string strSql = "select quanxian from [user] where name='" + Session["name"] + "'";
SqlCommand objCmd = new SqlCommand(strSql, objCon);
string quanxian= (string)objCmd.ExecuteScalar().ToString();
} 展开
2个回答
展开全部
ExecuteScalar()执行得到的内容有问题
一般都得加个判断是否为空
比如:
try
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
Object o = cmd.ExecuteScalar();
if ((o != null) && (o != DBNull.Value))
{ string quanxian = o.ToString(); }
}
catch (Exception ex)
{
throw;
}
一般都得加个判断是否为空
比如:
try
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
Object o = cmd.ExecuteScalar();
if ((o != null) && (o != DBNull.Value))
{ string quanxian = o.ToString(); }
}
catch (Exception ex)
{
throw;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询