请更正会话错误 [英] Please correct the error o session
本文介绍了请更正会话错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
查看我的代码。它在会话中存储Null值。
Hi Everyone,
Check My code. It stores Null Value in session.
for (int i = 0; i < GridView2.Rows.Count; i++)
{
string strID = GridView2.Rows[i].Cells[1].Text;
con.Open();
using (SqlCommand command = new SqlCommand("select Role_Name from Role2 where Role_ID=@strID", con))
{
SqlParameter param = new SqlParameter();
param.ParameterName = "@StrID"; param.Value = strID;
command.Parameters.Add(param);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Session["Role_Name"] = Convert.ToString(reader["Role_Name"]);
}
}
con.Close();
}
推荐答案
它们之间的基本区别是Convert.ToString(variable)
处理NULL值,即使变量值变为null但variable.ToString()
将不会处理NULL值,它将引发NULL引用异常错误。因此,使用转换的良好编码实践始终是安全的。 查看更多内容...... [ ^ ]
因此,最好使用.ToString()
来检查NULL
值。< br $> b $ b
-KR
The basic difference between them is "Convert.ToString(variable)
" handles NULL values even if variable value become null but "variable.ToString()
" will not handle NULL values it will throw a NULL reference exception error. So as a good coding practice using "convert" is always safe. see more...[^]
So it's better that you should use.ToString()
to checkNULL
values.
-KR
这篇关于请更正会话错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文