日期:2014-05-18  浏览次数:20442 次

ASP.NET 建立的Object,判斷空值的问题.
private   void   btnAddPeople_Click(object   sender,   System.EventArgs   e)  
{  
//lbDircetList.Items.Add(new   ListItem(GetChineseName().ToString(),GetEmployeeid().ToString()));  
string   sqlstr   =   " ";  
if(ddlAddPeople.SelectedValue   ==   "number ")  
{  
sqlstr= "select   Employeeid   From   Employees   where   Number= ' "+txtAddPeople.Text+ " ' ";  
}  
else  
{  
sqlstr= "select   Employeeid   From   Employees   where   ChineseName= ' "+txtAddPeople.Text+ " ' ";  
}  
SqlConnection   myConnection=new   SqlConnection(SQLCONNECTIONSTRING);  
SqlCommand   myCommand=new   SqlCommand(sqlstr,myConnection);  
myConnection.Open();  
object   EmployeeidObject=   " ";  
EmployeeidObject=myCommand.ExecuteScalar();  
myConnection.Close();  
lblMessage.Text   =   EmployeeidObject.ToString();  
}  
问题,如果EmployeeidObject为空值,就会出现 "Object   reference   not   set   to   an   instance   of   an   object.   ",提示引用对象.请问怎么解决?谢谢!


------解决方案--------------------
加个判断行么。
myConnection.Close();
if(EmployeeidObject!=null)
{
lblMessage.Text = EmployeeidObject.ToString();
}
------解决方案--------------------
lblMessage.Text = EmployeeidObject==null? " ":EmployeeidObject.ToString();
------解决方案--------------------
> >

lblMessage.Text = (EmployeeidObject == null ? " " : EmployeeidObject.ToString());
------解决方案--------------------
lblMessage.Text = (EmployeeidObject == null ? " " : EmployeeidObject.ToString());