日期:2014-05-20  浏览次数:20407 次

asp.net高分求助:Object reference not set to an instance of an object
我查了论坛里很多相关的问题,大概是对象没有实例化的问题,但还是不知道解决方法,希望得到大家的帮助!

Exception   Details:   System.NullReferenceException:   Object   reference   not   set   to   an   instance   of   an   object.

Source   Error:  

Line   82:  
Line   83:  
Line   84:                           string   teacherName   =   ds.Tables[ "teacherName "].NewRow().ToString();  

贴出源码如下:

          string   strSql   =   "SELECT   college.collegeName,   teacher.teacherID,   teacher.teacherName,   college.collegeID   FROM   teacher   INNER   JOIN   college   ON   teacher.collegeID   =   college.collegeID   WHERE   (teacher.teacherID   =   @teacherID) ";

          string   sconn=ConfigurationSettings.AppSettings[ "conn "];
          SqlConnection   conn=   new   SqlConnection(sconn);
          conn.Open();
          SqlParameter   para   =   new   SqlParameter( "@teacherID ",SqlDbType.NVarChar);

          para.Value   =Session[ "sUser "].ToString();
          SqlDataAdapter   da   =   new   SqlDataAdapter(strSql,conn);
          da.SelectCommand.Parameters.Add(para);

          DataSet   ds   =   new   DataSet();
          da.Fill(ds);
          string   teacherName   =   ds.Tables[ "teacherName "].NewRow().ToString();  
          conn.Close();
 

 


------解决方案--------------------
string teacherName;
if(ds.Tables[0].Rows.Count> 0&&ds.Tables[0].Rows[0][ "teacherName "]!=DBNull.Value)
teacherName = ds.Tables[0].Rows[0][ "teacherName "].ToString();